脚本超时:耗尽允许的执行时间

Script timeout: exhausted allowed execution time

当我尝试制作 运行 游戏中的硬币脚本时,输出显示 "Script timeout: exhausted allowed execution time"

脚本:

game.Players.PlayerAdded:Connect(function(player)

    local Coins = Instance.new("IntValue")
    Coins.Name = "Coins"
    local coinvalue = Coins.Value
    coinvalue = 0
    Coins.Parent = player
    wait(0.01)
    if player.Name == "Vlo_tz" then
        coinvalue = 25
    end
    wait(0.01)
    local cointext = game.StarterGui.SideGuis.InventoryFrame.CoinsTextValue
    while true do
    cointext = coinvalue
    end
end)

您的脚本执行时间太长,没有任何中断。 错误是抱怨这个循环没有退出案例:

while true do
    cointext = coinvalue
end

在循环内添加 wait() 可以消除错误,但看起来您正在使用它来保持某种 TextValue 更新。

更安全的方法是使用基于事件的回调。代替 运行 始终尝试更新 cointext 的循环,您可以监听 Co​​ins 值何时更改,然后调用一个函数来更新它。

game.Players.PlayerAdded:Connect(function(player)

    local Coins = Instance.new("IntValue")
    Coins.Name = "Coins"
    Coins.Value = 0
    Coins.Parent = player

    if player.Name == "Vlo_tz" then
        Coins.Value = 25
    end

    -- update the gui whenever Coins changes
    Coins.Changed:Connect(function()
        -- find the player's copy of the UI, if it has loaded
        -- (I'm assuming this is a TextValue and not a TextLabel)
        local coinText = player.PlayerGui.SideGuis.InventoryFrame.CoinTextValue

        -- keep this TextValue updated
        coinText.Value = tostring(Coins.Value)
    end)
end)