Script Exhausted allowed execution time

The error only started after I added the cloning, and the if’s for the levels; I tried fixing but to no avail… whats worse is when I add a wait(.1) it makes my game laggy. (It’s not the PC)

106b359970106ddaf862b62222707882a5462d42

local prevoiusLevel = nil
for i = 1, amountOfLevels do
	local level1 = script.Level:Clone()
	local level2 = script.Level2:Clone()
	local level3 = script.Level3:Clone()
	local floor2 = script.Floor2:Clone()

	local level
	level = level1

	level.Name = "Level" .. i


	if prevoiusLevel then
		if i > 5 then
			level1:Destroy()
			level = script.Level2:Clone()
		end
		if i > 15 then
			level2:Destroy()
			level = script.Level3:Clone()
		end
		if i > 25 then
			level3:Destroy()
			level = script.Floor2:Clone()
		end

		level:SetPrimaryPartCFrame(prevoiusLevel.LevelEnd.CFrame)
		level.WinsAmount.Value = (prevoiusLevel.WinsAmount.Value + 2) * 1.1
		level.LevelEnd.SurfaceGui.WinsAmount.Text = level.WinsAmount.Value
		local randomColour = BrickColor.random()
		for i, v in pairs(level:GetChildren()) do
			if v:IsA("BasePart") then
				v.BrickColor = randomColour
			end
		end
	end
	prevoiusLevel = level
	level.Parent = works:WaitForChild("Levels")

	level.LevelEnd.Touched:Connect(function(hit)
		local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		if not plr then return end

		local collectedWins = require(plr:FindFirstChild("collectedWins"))

		if table.find(collectedWins, level.Name) then return end

		plr.leaderstats.Wins.Value = plr.leaderstats.Wins.Value + level.WinsAmount.Value
		table.insert(collectedWins, level.Name)
	end)
end

Tell me if this fixes it i have noticed 4 problems in the script

local amountOfLevels = 30
local works = game.Workspace

local function createLevel(i, previousLevel)
	local level = nil
	local levelTemplate = nil
	local winsMultiplier = 1
	local destroyAfterLevel = false

	if i <= 5 then
		level = script.Level:Clone()
		levelTemplate = "Level"
	elseif i <= 15 then
		level = script.Level2:Clone()
		levelTemplate = "Level2"
	elseif i <= 25 then
		level = script.Level3:Clone()
		levelTemplate = "Level3"
	else
		level = script.Floor2:Clone()
		levelTemplate = "Floor2"
		destroyAfterLevel = true
	end

	level.Name = levelTemplate .. i
	level.Parent = works.Levels

	local levelEnd = level.LevelEnd
	if previousLevel then
		level:SetPrimaryPartCFrame(previousLevel.LevelEnd.CFrame)
		winsMultiplier = previousLevel.WinsAmount.Value * 1.1
		levelEnd.SurfaceGui.WinsAmount.Text = winsMultiplier
		levelEnd.SurfaceGui.Parent = levelEnd
		levelEnd.Transparency = 0.5
	end

	local randomColour = BrickColor.random()
	for _, part in ipairs(level:GetChildren()) do
		if part:IsA("BasePart") then
			part.BrickColor = randomColour
		end
	end

	levelEnd.Touched:Connect(function(hit)
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if not player then return end

		local collectedWins = require(player:FindFirstChild("collectedWins"))

		if table.find(collectedWins, level.Name) then return end

		player.leaderstats.Wins.Value = player.leaderstats.Wins.Value + winsMultiplier
		table.insert(collectedWins, level.Name)
	end)

	if destroyAfterLevel then
		wait(5)
		level:Destroy()
	end

	return level
end

local previousLevel = nil
for i = 1, amountOfLevels do
	local level = createLevel(i, previousLevel)
	previousLevel = level
end

  1. Uses a createLevel() function to create each level and encapsulate the code for each level.
  2. Optimizes the code to use ipairs() instead of pairs() where appropriate.
  3. Replaces WaitForChild() calls with local variables.
  4. Sets the SurfaceGui parent to the LevelEnd part after changing the text.
  5. Fixes a typo in works:WaitForChild() by changing it to works.Levels .
  6. Destroys the level after a short delay if it is the final level, to prevent excessive clutter in the workspace.

Sorry for the late response but it gave me this:

And the levels number multi just say “1.1”

This usually happens when a loop doesn’t have a wait to it. Assuming amountOfLevels is high, you need to optimize the code and add a task.wait to the loop. One way you could optimize this is to only clone a level when needed. Cloning and then immediately destroying is unnecessary and will reduce performance.

just add

wait(0.1)

at line 3 and the “Script Exhausted allowed execution time” error should go away.
it basically means you have a loop but its running too fast

I do it and it lags the game.

(30 limit)