roblox - 尝试在工作区中查找 ID 值时出现对象错误
问题描述
我试图通过使用一个值在工作区中找到一个对象。打印值时,它将值“WM830”打印为字符串。但它没有在工作区中找到模型。
这是代码:
script.Parent.MouseButton1Click:connect(function()
local busId = script.Parent.Parent.Parent.Car.value
game.Workspace[busId].Body.Display.maindest.SurfaceGUI.TextLabel.text = "Woop"
end)
这是错误:
Players.E_Link.PlayerGui.A-Chassis Interface.NXUI.TextButton.Script:3: bad argument #2 to '?' (string expected, got Object)
解决方案
它可以帮助您尝试将到 TextLabel 的路径分解为更可靠的块:
script.Parent.Activated:Connect(function()
-- find the name of the bus for this button
local busId = script.Parent.Parent.Parent.Car.value
local bus = game.Workspace:FindFirstChild(busId)
if bus then
-- if we find the bus, update the label on the bus
-- NOTE - IF THIS NEXT LINE FAILS, CONTINUE TO BREAK UP THIS PATH INTO SMALLER PIECES
local busDestLabel = bus.Body.Display.maindest.SurfaceGUI.TextLabel
busDestLabel.Text = "Woop"
else
-- Couldn't find the bus, check that the bus actually exists in the workspace
warn("Could not find a bus with id : ", busId)
end
end)
这样,您的错误消息可能会更接近路径的实际缺失部分。在尝试获取或设置属性之前验证您要使用的零件/模型是否确实存在也是一种很好的做法。
推荐阅读
- python - 如何制作交互式图像图(使用散景?)
- selenium - 如何使用 Selenium 从选择下拉列表中检索选项的值?
- javascript - 如何立即取消续订?
- c# - 将文件从机器 B 复制/流式传输到从机器 A 启动的机器 C
- java - 将小数转换为分数,java和python给出不同的输出
- sql - 避免 SQL select 语句中重复子查询的最佳方法
- python - 对象属性更改时的函数缓存
- javascript - 从数组中随机选择五个项目
- mysql - 如何在where条件下编写不等于的查询?
- fastify - Traefik 502 坏网关 | 错误的配置?| fastify nextjs