"attempt to index nil with 'WaitForChild'" 是什么意思?

script.Parent.MouseButton1Click:connect(function()
    local RS = game:GetService("ReplicatedStorage")
    local item = RS:WaitForChild("Pencil")
    local price = 350
    local player = game.Players.LocalPlayer
    local stats = player:WaitForChild("leaderstats")

    if stats.Strength.Value>=price then
        stats.Strength.Value = stats.Strength.Value - price
        local cloned = item:Clone()
        cloned.Parent = player.Backpack
        cloned.Parent = player.StarterGear
    end
end)

我正在尝试制作一个商店,但是在第6行出现了“attempt to index nil with 'WaitForChild'":

local stats = player:WaitForChild("leaderstats")

我按照视频中的方式复制了它,视频中没有问题,而且显然玩家没有值,尽管我们在它上面设置了一行

点赞
用户1442917
用户1442917

这意味着你正在索引一个 nil 值,也就是 player 值是 nil,也就意味着前一行的 game.Players.LocalPlayer 返回了 nil。为什么它会这样,你需要自己找原因,因为没有太多线索可供参考。

这个示例 显示应该是 local player = game:GetService("Players").LocalPlayer,所以你可能需要尝试一下。

2020-12-21 01:34:34