Roblox Studio - 在我的背包中出售“雪”时出现警告/错误

正如您在下面的图片中所看到的,当我进入圆圈时无法出售我背包中的物品。我遇到了这个错误/警告,但我无法找到问题所在。我已经发现在第5行出现了问题: player:WaitForChild

第5行的错误/警告:Players.asbjornbonde.PlayerGui:WaitForChild(“Stats”)可能会引起无限停机。

这是照片:

Screenshot

这是我的代码:

script.Parent.Touched:connect(function(Hit)
    local player = game.Players:FindFirstChild(Hit.Parent.Name)
    if player then
        local leaderstats = player:FindFirstChild(“leaderstats”)
        local PlayerGui = player:WaitForChild(“PlayerGui”):WaitForChild(“Stats”).Backpack
        if leaderstats and PlayerGui then
            local Cash = leaderstats:FindFirstChild(“Cash”)
            local snow = PlayerGui:FindFirstChild(“snow”)
            if Cash and snow then
                if snow.Value <= 0 then
                else
                Cash.Value = Cash.Value + 2 * snow.Value
                snow.Value = 0
                script.Parent.DigSound:Play()
                script.Disabled = true
                wait(0.1)
                script.Disabled = false
                end
            end
        end
    end
end)

我已经花了很多时间来尝试解决这个问题,但是真的无法解决。我希望能得到帮助。

点赞
用户5373986
用户5373986

Infinite yield possible on 是 Roblox Studio 中的一个警告,意味着如果等待的物体不存在或者永远不会被创建,那么你的脚本就可能会在那一行永远卡住。

由于唯一导致错误的WaitForChild是两个 WaitForChild连在一起的,所以我猜测脚本不喜欢你这样做。

第一个解决方案

如果你将两个 WaitForChild 分开成两个不同的变量,那么它就不会再给你报错了。不过这样做只是为了创造另一个变量,而没有真正的理由,所以我会选择第二种解决方案。

第二个解决方案

由于玩家已经加载并触碰了零件,可以安全地假定 GUI 已经加载好了,因此可以将你的 WaitForChild 更改为 FindFirstChild,如果你仍然想检查是否得到了 nil,也可以像这样引用它:player.PlayerGui.Stats.Backpack

希望这能帮到你。

2018-10-15 15:36:13
用户10450122
用户10450122

将下面翻译成中文并且保留原本的 markdown 格式,

WaitForChild() 可以视为一个循环。它一直在后台快速执行,尽可能快地执行 Lua 引擎。建议不要在 Roblox Lua 中使用此功能或任何未受控制的循环,因为引擎处理不好。

2018-10-16 13:47:12
用户10649630
用户10649630

如果这是一个启用过滤器的服务器脚本,问题在于服务器无法访问 PlayerGui 的现有成员。为了解决这个问题,您需要在客户端上运行此代码(进行必要的更改),或者您需要使用远程事件!

2018-11-14 03:56:05