Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

[SOLVED] How do I fix this error, "Script timeout: exhausted allowed execution time?"

THIS QUESTION HAS BEEN SOLVED

Information:

  • I am getting an error on line 5. The error is "Script timeout: exhausted allowed execution time"
  • I am trying to do a coin spawning script
  • I want to spawn 5 coins when no coins can be found in game.Workspace.Coins
  • I am cloning from game.ReplicatedStorage.Coins
  • The name of the coins are "Coin"

Script:

while true do
	local c = game.Workspace.Coins:FindFirstChild("Coin")
	if not c then
		wait()
			for i,coin in pairs(game.ReplicatedStorage.Coins:GetChildren()) do
				local clone = coin:Clone()
    		clone.Parent = game.Workspace.Coins
		end
	end
end

1 comment

0

when doing while true do you need a wait() that will always happen

2 answers

✓ Accepted answer

Same concept as the other answer, but here is an even simpler way to do this (I also would not recommend while true loops, but for some things it's ok):

while wait() do --just have the wait in the condition and it will wait then loop. Its quick and easy while also not breaking anything.
    local c = game.Workspace.Coins:FindFirstChild("Coin")
    if not c then
            for i,coin in pairs(game.ReplicatedStorage.Coins:GetChildren()) do
                local clone = coin:Clone()
            clone.Parent = game.Workspace.Coins
        end
    end
end

1 comment

0

Works perfectly!

You need to add a wait() so that your script does't get over whelmed. Basically your code is running too many times a second, and this causes an error so that Roblox Studio doesn't crash. Even if it is a 0.5 second wait that will stop your code from running hundreds of times ever second. Here is what your code should look like now:

while true do
    local c = game.Workspace.Coins:FindFirstChild("Coin")
    if not c then
        wait()
            for i,coin in pairs(game.ReplicatedStorage.Coins:GetChildren()) do
                local clone = coin:Clone()
		wait(--time to wait)
            clone.Parent = game.Workspace.Coins
        end
    end
end

If this does help you make sure to accept my answer. Good luck with your project.

This discussion is preserved as it was. New answers, comments, and votes are closed.