如何使用 RemoteEvent 从服务器向客户端发送参数?Roblox

我的目标是将玩家对象和对象名称发送到客户端脚本,以在文本标签上显示对象名称。 当我运行此代码时,它打印出 nil 和 nil。我想要的是 player.Name 和 name,如您将在第二段代码示例中看到的那样。另一个错误是来自客户端脚本的“尝试将 nil 与字符串连接”错误。

以下是我的服务器端代码:

script.onTouch.OnInvoke = function(button, sellObj, sellObjValue, buyObj, buyObjValue, afterWave, isLoadedPart)
    if isLoadedPart == true then
        local info = script.Parent.Parent.info
        local player = info.player.Value
        local owner = info.owner.Value
        local savedItems = info.savedItems.Value
        local builds = script.Parent.Parent.activeBuilds
        if afterWave > info.activeWave.Value then
            info.activeWave.Value = afterWave
        end
        button.Parent = savedItems.buttons
        button.jobDone.Value = true
        if sellObj ~= nil then
            sellObj.Parent = savedItems.builds
        end
        if buyObj ~= nil then
            buyObj.Parent = builds
        end
        local td = require(script.Parent.tycoonDictionary)
        if not table.find(td.boughtButtons, button.objectId.Value) then
            table.insert(td.boughtButtons, button.objectId.Value)
        end
        local ui = game.ReplicatedStorage:FindFirstChild('onPartLoad')
        if ui then
            ui:FireClient(player, 'buyObj.Name')
            print('yes')
        else
            print('no')
        end
    else
        local info = script.Parent.Parent.info
        local player = info.player.Value
        local owner = info.owner.Value
        local money = info.player.Value.leaderstats.Money
        local savedItems = info.savedItems.Value
        local builds = script.Parent.Parent.activeBuilds
        if money.Value >= buyObjValue or money.Value == buyObjValue then
            if afterWave > info.activeWave.Value then
                info.activeWave.Value = afterWave
            end
            button.Parent = savedItems.buttons
            button.jobDone.Value = true
            if sellObj ~= nil then
                sellObj.Parent = savedItems.builds
                money.Value += sellObjValue
            end
            if buyObj ~= nil then
                buyObj.Parent = builds
                money.Value -= buyObjValue
            end
            local td = require(script.Parent.tycoonDictionary)
            if not table.find(td.boughtButtons, button.objectId.Value) then
                table.insert(td.boughtButtons, button.objectId.Value)
                warn(td.boughtButtons)
            end
        else
            player.PlayerGui.inGame.error.label.invokeScript.errorInvoke:Invoke("Insufficient Funds")
        end
    end
    script.Parent.waveChecker.afterRun:Invoke()
end

以下是我的客户端代码:

game.ReplicatedStorage.onPartLoad.OnClientEvent:Connect(function(player, name)
    print(player.Name, name)
    print(script.Parent.Text)
    script.Parent.Text = name .. 'is loaded.'
    print(script.Parent.Text)
end)

在这里,我将向您介绍一下这个游戏的一些事情。它是一个使用包含所有按钮 id 的表格保存数据的 tycoon 游戏。当它加载时,它获取与 id 关联的按钮并为每个按钮触发服务器代码。如果按钮是加载按钮,则使用 player 和 buyObj.Name 触发客户端。

是否只是有点错误,还是根本无法向客户端发送参数?任何帮助都将不胜感激!

原文链接 https://stackoverflow.com/questions/70545934

点赞
stackoverflow用户12265974
stackoverflow用户12265974

OnInvoke 回调函数的第一个参数始终为触发它的玩家,因此您应该更改第一个脚本的第一行如下:

script.onTouch.OnInvoke = function(playerFired, button, sellObj, sellObjValue, buyObj, buyObjValue, afterWave, isLoadedPart)

:FireClient() 要求将玩家作为第一个参数,因此您应该将

ui:FireClient(player, 'buyObj.Name')

更改为

ui:FireClient(playerFired, player, 'buyObj.Name')
2021-12-31 22:16:46
stackoverflow用户17246167
stackoverflow用户17246167

这是我想出的解决方案;

首先,我阅读了一些 Roblox 文档,发现在使用 :FireClient 时必须发送的第一个参数是玩家,因为我已经有了玩家,所以它只是将函数发送到该玩家。现在,我有两种选择,我可以两次发送玩家,或者从脚本中删除玩家变量。我选择第二个。

这是服务器脚本中的 :FireClient 行现在的样子:

game.ReplicatedStorage:WaitForChild('onPartLoad'):FireClient(player, buyObj.Name)

这是客户端函数脚本现在的样子:

game.ReplicatedStorage.onPartLoad.OnClientEvent:Connect(function(name)
    if name ~= 'starterFoundation' then
        script.Parent.Text = name .. ' is loaded.'
    end
end)

感谢 @TypeChecked 帮我意识到这一点!

2022-01-01 15:32:33