Roblox Studio 错误:Workspace.Driver.OnSeated!:13: 尝试用'Name'索引空值

我在 Roblox Studio 中的一个脚本中遇到了错误。显示问题的行是第 13 行。该脚本的作用是当有人坐在座位上时进行焊接,并启动 TankGUI(另一个正常的脚本)。

seat = script.Parent

function onChildAdded(part)
    if (part.className == "Weld") then
        while true do
local           welde = seat:FindFirstChild("SeatWeld")

            if (welde ~= nil) then
local           sitted = welde.Part1.Parent
            end

            if (sitted.Name ~= script.Parent.Owner.Value) then
                if (script.Parent.Owner.Value == "NoOne") then
                    script.Parent.Owner.Value = sitted.Name
                    print ("sitted steal")
                else
                    print ("stolen!!")
local                   shipstealer = game.Workspace:FindFirstChild(sitted.Name)
                    if (shipstealer ~= nil) then
                        shipstealer.Humanoid.Jump=true
                    end
                end
            end

            wait(0.2)

            if (welde == nil) then
                print("BreakLoop")
                script.Parent.Owner.Value = "NoOne"
            break end
        end
    end
end

--while true do
--  wait(0.5)
--  script.Parent.Parent.Name=script.Parent.Owner.Value .. "'s Ship"
--end
seat.ChildAdded:connect(onChildAdded)

请原谅我任何不好的配合或语言障碍。我还是有点新手在这里。

点赞
用户7509065
用户7509065
如果您声明`sitted`为`local`,则在 `end` 时超出了其范围,因此当您尝试在`if`中使用它时,实际上使用的是一个空的全局变量。因此,请使用以下代码:
        local sitted
        if (welde ~= nil) then
            sitted = welde.Part1.Parent
        end

        if (sitted.Name ~= script.Parent.Owner.Value) then

```

这样它就会一直保持在范围内,直到您真正用完它。

2020-05-01 14:59:39