尝试对索引值为 'player' 的 upvalue 进行索引,其值为 nil

求救!我正在制作一个游戏,但一直遇到同样的问题

我尝试了很多方法,但没有一种有效


script.Parent.Humanoid.Died:Connect(function()
    print("yeet")
    player.leaderstats.PuzzlePieces.Value =  player.leaderstats.PuzzlePieces.Value + 1
end)
点赞
用户2860267
用户2860267

错误:attempt to index upvalue 'player' (a nil value) 意味着您试图使用未定义的变量,一般指“player”。因此,您只需要通过指向game.Players中正确的对象来创建玩家变量。

我假设您将此脚本放在一个玩家模型中。

玩家模型和人型模型位于 game.Workspace 中,而 leaderstats 对象位于 game.Players 中的一个对象中。您需要使用这两个对象互相通信。

local playerModel = script.Parent
playerModel.Humanoid.Died:Connect(function()

    -- 使用玩家的名称在 game.Players 中找到玩家对象
    local playerName = playerModel.Name
    local player = game.Players[playerName]

    -- 更新排行榜
    player.leaderstats.PuzzlePieces.Value =  player.leaderstats.PuzzlePieces.Value + 1
end)

希望这可以帮助您。

2019-05-24 18:25:48