首页 > 解决方案 > 尝试在 roblox studio 中使用“leaderstats”索引 nil

问题描述

local garbage = game.Teams["Glizzy Garbage"]
local player = game.Players.LocalPlayer

if player.leaderstats.Pounds.Value <= 1000 then  --this is the line that the output is detecting the error
    player.Team = garbage
end

我试图让它在玩家达到一定数量的“磅”时,他们将自动获得滚动。我搜索了许多 youtube 视频,但没有找到解决方法或替代方法,我不确定为什么这不起作用。此脚本位于工作区中。感谢所有帮助。

标签: luarolesrankrobloxleaderboard

解决方案


LocalPlayer对象仅在 LocalScripts 中公开。因为您在工作区中使用脚本,所以您必须以player另一种方式访问​​该对象。在 Pounds 值更改时触发的函数中处理这种逻辑也是一个好主意。

尝试使用game.Players.PlayerAdded信号:

local garbage = game.Teams["Glizzy Garbage"]

game.Players.PlayerAdded:Connect(function(player)
    local Pounds = player.leaderstats.Pounds
    Pounds.Changed:Connect(function(value)
        if value <= 1000 then
            player.Team = garbage
        end
    end)
end)

推荐阅读