为什么我的RemoteFunction无法在PlayerAdded事件中触发

时间:2020-04-19 22:13:15

标签: lua roblox

从UI中选择一个类后,客户端将消息发送到服务器。服务器设置默认的DataStore2值,并侦听更新以将信息发送回客户端。我的想法是让RemoteFunctions的服务器调用将类添加到数据存储中,然后将该类发送回客户端(用于stats 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

0 个答案:

没有答案
相关问题