脚本超时:超出允许的执行时间

当我试图制作并运行我的游戏中的硬币脚本时,输出说“脚本超时:超出允许的执行时间”

脚本:

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)
点赞
用户2860267
用户2860267

你的脚本在没有任何休息的情况下执行的时间太长了。

错误指出这个循环没有退出条件:

while true do
    cointext = coinvalue
end

在循环内添加 wait() 可以消除这个错误,但是似乎你正在使用它来更新某种 TextValue。

一种更安全的方法是使用事件回调。而不是运行一个总是尝试更新 cointext 的循环,你可以监听 Coins 值何时发生变化,然后调用一个函数来更新它。

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

    -- 每当 Coins 改变时更新 gui
    Coins.Changed:Connect(function()
        -- 找出玩家的 UI 副本,如果已经加载了(我假设这是 TextValue 而不是 TextLabel)
        local coinText = player.PlayerGui.SideGuis.InventoryFrame.CoinTextValue

        -- 将该 TextValue 更新为 Coins 的值的字符串形式
        coinText.Value = tostring(Coins.Value)
    end)
end)
2020-06-04 03:24:11