尝试在工作区中查找 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)

这样,您的错误消息可能会在接近实际缺少的路径部分时失败。在尝试获取或设置属性之前,验证要使用的零件/模型是否确实存在也是一种很好的做法。

最新更新