Going to start of a loop

The only plausible way I can think of going back to the start of a loop would be something like this:

function RunLoop()
	local Index = 0
	
	while true do
		if Index >= 3 then
			print("Restarting loop, index will start a 1.")
			RunLoop()
			break
		end
		Index = Index + 1
		print(Index)
		wait(1)
	end
end

RunLoop()

If you were to let that code run you will notice this output:

  1
  2
  3
  Restarting loop, index will start a 1.
  1
  2
  3

Essentially you would want a function that starts the loop, and whenever you plan to restart the loop you would run the function (which will rerun the loop) and then do a break which stops the active loop.

7 Likes