如何使用 RemoteEvent 从服务器向客户端发送参数?Roblox
2021-12-31 22:12:4
收藏:0
阅读:335
评论:2
我的目标是将玩家对象和对象名称发送到客户端脚本,以在文本标签上显示对象名称。 当我运行此代码时,它打印出 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用户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
评论区的留言会收到邮件通知哦~
推荐文章
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
- 如何编写 Lua 模式将字符串(嵌套数组)转换为真正的数组?
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')