如何用lua在Roblox中检查对象的存在性?
2020-1-26 12:13:20
收藏:0
阅读:171
评论:2
我正在尝试编写一个动态分配的GUI。我有四个团队。我卡在一个特定点上。我想要一个函数,当玩家加入游戏时,检查其他团队是否已经得分以更新他们的标签。它看起来像这样:
local function updateAllLabelsLateArrival(redPoints, bluePoints, yellowPoints, greenPoints)
game.Players.LocalPlayer.PlayerGui.ScreenGui.ReallyRedTeam.Points.Text = redPoints
game.Players.LocalPlayer.PlayerGui.ScreenGui.ReallyBlueTeam.Points.Text = bluePoints
game.Players.LocalPlayer.PlayerGui.ScreenGui.NewYellerTeam.Points.Text = yellowPoints
game.Players.LocalPlayer.PlayerGui.ScreenGui.LimeGreenTeam.Points.Text = greenPoints
end
该函数是在玩家加入时从服务器端脚本远程触发的。我的问题是,并不是所有四个标签都可能存在。假设当已经有一个红队玩家在玩时,绿队球员加入,它将返回错误
ReallyBlueTeam不是ScreenGui的有效成员
我想要将每一行包装在if语句中来检查标签是否存在,就像这样:
if game.Players.LocalPlayer.PlayerGui.ScreenGui.ReallyRedTeam then game.Players.LocalPlayer.PlayerGui.ScreenGui.ReallyRedTeam.Points.Text = redPoints end
但是这会导致相同的错误。所以我的问题是,我如何检查标签是否已被创建以更新分数?谢谢
点赞
用户5373986
如果你想让它们都在一行上,那么最好使用 FindFirstChild(),就像 @jjwood1600 所说的那样。我还建议使用变量来缩短你的GUI路径,正如下面所示:
local function updateAllLabelsLateArrival(redPoints, bluePoints, yellowPoints, greenPoints)
local userGui = game.Players.LocalPlayer.PlayerGui.ScreenGui
if userGui:FindFirstChild("ReallyRedTeam") then userGui.ReallyRedTeam.Points.Text = redPoints end
if userGui:FindFirstChild("ReallyBlueTeam") then userGui.ReallyBlueTeam.Points.Text = bluePoints end
if userGui:FindFirstChild("NewYellerTeam") then userGui.NewYellerTeam.Points.Text = yellowPoints end
if userGui:FindFirstChild("LimeGreenTeam") then userGui.LimeGreenTeam.Points.Text = greenPoints end
end
在普通Lua中,你确实可以像你所做的那样使用 if 语句,不使用 FindFirstChild。但是,Roblox自己的版本RBX.Lua不支持这种方式。
2020-01-27 11:02:17
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在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 代码?

如果这是一个本地脚本,你可以使用
WaitForChild(),它将等待直到该标签被创建!game.Players.LocalPlayer.PlayerGui:WaitForChild("ScreenGui"):WaitForChild("ReallyRedTeam"):WaitForChild("Points").Text = redPoints关于
WaitForChild的更多信息在这里!或者,如果你不确定它们是否被创建,你可以使用
FindFirstChild。这不会挂起。if game.Players.LocalPlayer.PlayerGui.ScreenGui:FindFirstChild("ReallyRedTeam") then print("它存在") end关于
FindFirstChild的更多信息在这里!希望能帮到你!