Vlo_tz
Vlo_tz

Reputation: 21

Script timeout: exhausted allowed execution time

When I was trying to make and run a script for coins in my game the output said "Script timeout: exhausted allowed execution time"

Script:

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)

Upvotes: 0

Views: 1617

Answers (1)

Kylaaa
Kylaaa

Reputation: 7188

Your script is executing for too long without any kind of break. The error is complaining that this loop has no exit case :

while true do
    cointext = coinvalue
end

Adding a wait() inside the loop would get rid of the error, but it looks like you're using it to keep some kind of TextValue updated.

A safer way to do this is with event based callbacks. Instead of running a loop that will always try to update the cointext, you can listen for when the Coins value changes and then call a function to update it instead.

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)

Upvotes: 1

Related Questions