"Script timeout: exhausted allowed execution time" for an odd reason

I have a function that generates a 5 stage obby, but it gives the error “Script timeout: exhausted allowed execution time”

local stages = {}
function generatestages()
	repeat 
		local stage = game.ReplicatedStorage.Stages:GetChildren()[math.random(1, #game.ReplicatedStorage.Stages:GetChildren())]
		if not table.find(stages_added, stage.Name) then
			
			local stagenew = stage:Clone()
			table.insert(stages_added, stage.Name)
			stagenew.Parent = workspace.StagesCopy			
			stagenew:PivotTo(workspace.ObbyAreas["ObbyArea".. #stages_added].CFrame)
		end
	until #stages_added == 5
end

Even when I add a wait or a task.wait to the loop it generates 2-3 stages then errors again.
Also for some reason, this line:
stagenew.Parent = workspace.StagesCopy
is the reason for the error. If I instead parent stagenew to just workspace it doesn’t error. I don’t know why

2 Likes

Try #stages_added >= 5 instead, if that doesn’t work then check if the function itself is being spammed in an infinite loop.

How many stages are located in game.ReplicatedStorage.Stages? If there are less than 5, it will never end the loop

There are only 5 stages in the Stages folder

Try adding a task.wait() somewhere within the loop, and add a print(#stages_added) within the loop too. What output does that give/

Doesn’t work. Im 100% sure its not because of the repeat loop inside of the function because even when I remove the loop and make it:

local stages = {}
function generatestages()
	local stage = game.ReplicatedStorage.Stages:GetChildren()[math.random(1, #game.ReplicatedStorage.Stages:GetChildren())]
	if not table.find(stages_added, stage.Name) then
			
		local stagenew = stage:Clone()
		table.insert(stages_added, stage.Name)
		stagenew.Parent = workspace.StagesCopy			
		stagenew:PivotTo(workspace.ObbyAreas["ObbyArea".. #stages_added].CFrame)
	end
end

It still doesn’t work. But ill check if its being run in an infinite loop

What is stages? (defined at the top of the script).
Is there another table called stages_added elsewhere in the script?

apologies, I added the stages table at the top of the post so people would know im referencing a table. I meant stages_added. In the actual script it doesn’t say stages = {}, it says stages_added = {}

when I add a task.wait it still errors. even if i make it one second it still errors. When I print the #stages_added. it prints 1 twice and then I get the error

Maybe try this?

local stages = {}
local stages_added = {} -- Initialize the stages_added table

function generatestages()
    local maxStages = 5 -- Set the maximum number of stages

    while #stages_added < maxStages do
        local stage = game.ReplicatedStorage.Stages:GetChildren()[math.random(1, #game.ReplicatedStorage.Stages:GetChildren())]
        if not table.find(stages_added, stage.Name) then
            local stagenew = stage:Clone()
            table.insert(stages_added, stage.Name)
            stagenew.Parent = workspace.StagesCopy
            stagenew:PivotTo(workspace.ObbyAreas["ObbyArea".. #stages_added].CFrame)
        end
    end
end

-- Call the function
generatestages()

still errors. even when removing the loop the function is called in and the repeat until loop it still errors

I don’t think it has anything to do with the loops. Since it errors when I parent the stage clones to the StagesCopy folder, but not when I parent it to workspace

Update, the reason for the issue wasnt even the script the function was in. One of the stages had a script which was infinitely looping to find its parent but since I was trying to change it it would timeout.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.