为什么我的 RemoteFunction 在 PlayerAdded 事件中不起作用?

客户端在从 UI 中选择类之后向服务器发送消息。服务器设置默认的 DataStore2 值并监听更新以发送信息回到客户端。我的想法是让 RemoteFunctions 的服务器调用将类添加到数据存储,并将该类发送回客户端(用于统计 gui)。该函数不会在 PlayerAdded 中触发。但是该函数在事件外部确实触发,但 Remote 执行中的 callRemote() 函数需要事件中的变量。有什么建议吗?

--server
data.Combine("DATA","gold","sh_coins","class")

spawn(function()
    glbs.Plrs.PlayerAdded:connect(function(Plr)

        local goldstore = data("gold",Plr)
        local shcoinstore = data("sh_coins",Plr)
        local classstore = data("class",Plr)

        function callRemote(remote,value)
            sendDataToClient:InvokeClient(Plr,remote,value)
        end

        callRemote('Gold',goldstore:Get(500))
        callRemote('Stronghold Coins',shcoinstore:Get(0))
        callRemote('Class',classstore:Get('None'))

        goldstore:OnUpdate(callRemote)
        shcoinstore:OnUpdate(callRemote)
        classstore:OnUpdate(callRemote)

        classSelected.OnServerInvoke = function(plr,Class)
            print('class remote called: '..Class)
            callRemote('Class',classstore:Set(Class))
        end

    end)
end)
--client
local function connectClasses() --to play button
    for _,v in pairs(menuclass:GetChildren()) do
        wait()
        if v:IsA('TextButton') then
            if v.Name ~= 'Back' then
                local inf = tinfo(.5,Enum.EasingStyle.Elastic)
                spawn(function()
                    v.Button.MouseButton1Click:connect(function()
                        --print('clickitied')
                        spawn(function()
                            ClassSelected:InvokeServer(v.Name)
                        end)
                        local goal = {}
                        goal.Position = menuout
                        local t0 = twn(menuclass,inf,goal)
                        t0.Completed:wait()
                        gbls.Debris:AddItem(startmenu,0)
                        spawned = true
                        main.Enabled = true
                        cam.CameraType = Enum.CameraType.Custom
                    end)
                end)
            end
        end
    end
end

--update stats on data load (handled by server)
function updateStats(stat,val)
    menuf[stat].Text = stat..': '..val
end
--listen for data load
StatUpdate.OnClientInvoke = updateStats
点赞