Script Exhaust (Exhausted allowed execution time)

RobloxPlayerBeta_0SPxqdOVPp
Could anyone help me with this issue, thanks! Goal: stop the script from exhausting!
Code (pretty sure it’s this part):

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

You have a loop somewhere that is running infinitely. If it is a while loop, add a wait() to the end to fix.

while true do
--code
    task.wait()
end

Sadly it didn’t work, any other fixes?

You have for loops inside of the while loop. This could be causing the issue?

Fixed it, the issue was the if’s, I was lazy/forgot and didn’t put elseif. Silly me!

What did you do with the ifs to fix this issue?