Workspace.Arrow.StandGiver:38: 尝试使用“Destroy”索引nil

我的目标是,当您单击箭头时,Players中的文件夹而不是Stand1应更改为Stand2,但这并没有发生。 尝试解决,但无济于事。

此行中的错误:

prevStand:Destroy()

local Arrow = script.Parent
local Click = Arrow:WaitForChild("Click")

Click.MouseClick:Connect(function(player)

    local Backpack = player:FindFirstChild("Backpack")
    if Backpack then
        local Stands = game:GetService("ServerStorage"):WaitForChild("Stands")

        local PlrStats = player:WaitForChild("PlrStats")
        local currentStand = PlrStats:WaitForChild("Stand")

        local randomStand = math.random(1,2)
        if randomStand == 1 then
            --Give Stand1
            if Backpack:FindFirstChild("Stand1") or Backpack:FindFirstChild("Stand2") then
                local prevStand = workspace:FindFirstChild(player.Name.." Stand")
                local prev = Backpack:FindFirstChild("Stand1") or Backpack:FindFirstChild("Stand2")
                prevStand:Destroy()
                prev:Destroy()

                local Stand = Stands:FindFirstChild("Stand1"):Clone()
                Stand.Parent = Backpack

                currentStand.Value = "Stand1"
            else
                local Stand = Stands:FindFirstChild("Stand1"):Clone()
                Stand.Parent = Backpack

                currentStand.Value = "Stand1"
            end

        elseif randomStand == 2 then
            --Give Stand2
            if Backpack:FindFirstChild("Stand1") or Backpack:FindFirstChild("Stand2") then
                local prevStand = workspace:FindFirstChild(player.Name.."Stand")
                local prev = Backpack:FindFirstChild("Stand1") or Backpack:FindFirstChild("Stand2")
                prevStand:Destroy()
                prev:Destroy()

                local Stand = Stands:FindFirstChild("Stand2"):Clone()
                Stand.Parent = Backpack

                currentStand.Value = "Stand2"
            else
                local Stand = Stands:FindFirstChild("Stand2"):Clone()
                Stand.Parent = Backpack

                currentStand.Value = "Stand2"
            end
        end
        Arrow:Destroy()
    end
end)

我有带有Stand1和Stand2的文件夹,不知道怎么解决。

原文链接 https://stackoverflow.com/questions/70746205

点赞
stackoverflow用户2858170
stackoverflow用户2858170

如果 prevStandnil,那么 workspace:FindFirstChild(player.Name.."Stand") 显然会返回 nil

如果没有该名称的子对象,或者在本地脚本中,如果该实例尚未复制,就会发生这种情况。

尝试使用WaitForChild代替。

2022-01-17 21:10:38