首页 > 解决方案 > 尝试在工作区中查找 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)

标签: roblox

解决方案


它可以帮助您尝试将到 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)

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


推荐阅读