【问题标题】:attempt to index nil with 'leaderstats' in roblox studio尝试在 roblox studio 中使用“leaderstats”索引 nil
【发布时间】:2020-11-22 12:05:35
【问题描述】:
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 视频,但没有找到解决方法或替代方法,我不确定为什么这不起作用。此脚本位于工作区中。感谢所有帮助。

【问题讨论】:

    标签: lua roles rank roblox leaderboard


    【解决方案1】:

    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)
    

    【讨论】:

    • 这只会在玩家加入游戏时触发。我假设他们想观察 leaderstats.Pounds 的价值。所以在增加磅数时可能应该进行团队切换
    • 啊,不错。我将使用处理程序更新值更改。
    【解决方案2】:

    我的只是

    local plrStage = plr.leaderstats.Stage.Value
    

    【讨论】:

    • 请不要只发布代码作为答案,还要解释您的代码的作用以及它如何解决问题的问题。带有解释的答案通常更有帮助,质量更高,更有可能吸引投票。
    猜你喜欢
    • 2020-12-16
    • 1970-01-01
    • 2022-01-10
    • 2022-01-05
    • 2022-10-13
    • 2022-10-24
    • 2021-09-23
    • 2020-08-17
    • 2020-08-16
    相关资源
    最近更新 更多