Script exhausted problem!

for some reason this script got the error “Script timeout: exhausted allowed execution time”

this is the script:

repeat
		local newAmountRows = #platesFolder:GetChildren()
		
		local rowToClone = platesFolder:FindFirstChild("Row"..tostring(newAmountRows))
		
		local rowClone = rowToClone:Clone()
		
		rowToClone.Parent = platesFolder
		
		for i,v in pairs(rowClone:GetChildren()) do
			if v.Name == "Row" then
				v.Value += 1
			elseif v.Name == "Plate" then
				local currentPlate = v
				local currentLine = v.Line.Value
				local thisRow = v.Parent.Row.Value
				local colorRowToSearch = thisRow - 2
				local otherColorModel = platesFolder:FindFirstChild("Row"..tostring(colorRowToSearch))
				
				local positionRowToSearch = thisRow - 1

				local otherPositionModel = platesFolder:FindFirstChild("Row"..tostring(positionRowToSearch))
				
				for i,v in pairs(otherPositionModel:GetChildren()) do
					if v.Name == "Plate" then
						for i,v in pairs(v:GetChildren()) do
							if v.Name == "Line" then
								if v.Value == currentLine then
									local otherPositionPart = v.Parent

									local newPositionX = otherPositionPart.Position.X + 9
									local newPositionY = otherPositionPart.Position.Y
									local newPositionZ = otherPositionPart.Position.Z
									currentPlate.CFrame = CFrame.new(newPositionX, newPositionY, newPositionZ)
								end
							end
						end
					end
				end
				
				for i,v in pairs(v:GetChildren()) do
					if v.Name == "Line" then	
						for i,v in pairs(otherColorModel:GetChildren()) do
							if v.Name == "Plate" then
								for i,v in pairs(v:GetChildren()) do
									if v.Name == "Line" then
										if v.Value == currentLine then
											local otherColorPart = v.Parent
											currentPlate.Color = otherColorPart.Color
										end
									end
								end
							end
						end
					end
				end
			end
		end
		
	until #platesFolder:GetChildren() == finalRowsAmount

The reason is cause there is to much running in this one script.
Try adding some task.Wait()'s in there!
That will help!

If you’re making a grid of some kind, then your loop is never ending for some reason. Maybe you forgot to parent something to platesFolder? This overall looks like it could be a lot more efficient though.

You need to optimize this script. I see triple-nested quadruple-nested for loops inside of a repeat until loop which isn’t a good indicator of performance. Scripts can only execute for so long until they are forcibly killed by Studio; your game would crash otherwise. Adding some well-placed task.wait() methods will probably fix this issue, but that’s a band-aid fix which doesn’t address the general inefficiency of the script.

At a quick glance, it seems like CollectionService could possibly clean things up here. Reminder that we’re not responsible for writing better code for you.