Is there a better way to make a while loop

hello guys, i’ve been wondering is there a such better way to make a while loop

there’s only way to stop a while loop is a condition(“if statements” or if a value is true or not),
its either at the start of the loop or inside of it

the only problem i have with using conditions to stop a while loop is,

local condition = false

while condition == true do -- while condition is true
	print("first") --prints immediately 
	wait(10)
	print("last") --waits to be printed
end

for this loop if you change the condition to false while the loop is still running it will still print “last”
it’ll only stop once it reaches end and the condition is false

its the same when you have the condition inside the loop

while true do
	print("first")
	wait(10) --has to wait until it reaches the condition to be able to stop
	print("last")
	if condition == false then break end -- either return or break does the same thing to stop the loop
end

for this loop it also has to wait until it reaches the condition to stop running

and there is also one other loop
the repeat until, its also does basically the same thing such as the other loops

repeat
	print("first")
	wait(10)
	print("last")
until condition == false

there’s also another problem with loops
the code underneath the loop won’t execute

while true do
	print("hey")
	wait(10)
end
print("done!") -- doesn't print

so what do all these loops have in common?

  • well first of all if you put code underneath the loop, the code doesn’t execute

  • second, there has to be a condition until the loop can be able to stop, meaning it has to wait until the script reaches to the condition to be able to be stopped

  • and lastly to me it just gives me a headache using these loops and a reckoning of an unstoppable force

I tried to fix these problems by using runserivce into the mix, but im not too sure if this is the best method of making an alternative while loop, im just wondering is there a better way to solve this problem?

this is what i tried doing

local RunService = game:GetService("RunService")

local cooldown = false

local function loop()
	if cooldown == true then return end --debounce/cooldown
	cooldown = true
	print("hi")
	task.wait(1)
	cooldown = false
end

print("hello")

local loopConnect = RunService.Heartbeat:Connect(loop) --be able to make a connection
task.wait(10)

loopConnect:Disconnect() --disconnecting the connection

thanks for reading this, and please help spare the pain

Not really a solution but I think you should optimize loops to a particular problem and not just to the general. With a well written algorithm designed to solve a particular issue the above should not really be a problem.

checks if condition is false every 0.1 seconds, if it is, then the loop breaks. also still prints last every 10 seconds

just a concept u shouldnt use this its probably not good for your games performance

local condition = false

while condition == true do
	local counter = 0
	
	print ("start")
	
	while true do
		counter = counter + 0.1
		
		if (counter == 100) -- if 10 seconds has passed
		then
			print("end")
			break
		end
		
		if (condition == false) -- constantly check if condition is false false
		then
			break
		end
		
		wait(0.1)
	end
	
	if (condition == false) 
	then
		break
	end
	
end

yeah that might lag quite a bit

when i think about it, i don’t think it’s going to lag any more than a regular while loop, as there is only 1 loop is running at a time in that script.

The solution here is to only use them when they are needed. And when they are needed, it will be a situation where you know the loop will reach a false condition eventually.

Example

while player.Character == nil do 
    wait(1)
end
doSomethingWithCharacter(player.Character) --We don't want this code to run until loop ends cause that means the players character isn't loaded

If you need to run a loop at the same time as another piece of code, look into coroutines

While loops, and loops by design, aren’t meant to run infinitely. They’re meant to execute until some condition is met. They’re great structures if you use them wisely. A lot of people think infinite loops do the work, but it’s viewed upon as bad design. A loop structure should always have an exit point.

RunService events are by far the more superior option. Truth be told, they’re not loops at all. They just invoke any attached callbacks (called listener functions) when the event fires, so it acts like a loop, but it isn’t at all. It executes at a given point in the current frame. (Heartbeat, Stepped, and RenderStepped all fire at different times.)

2 Likes