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
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
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
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()
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.