Should I Use While True Do Or While Wait() Do?

Hi So About A Year Ago Or 2 There Was Like This Big Controversy Regarding While Wait() Do. I Have Been Confused For Very Long Which I Should Be Using. Some Say To Use While Wait() Do And Others While True Do. So Which Should I Use Then? Thank You For Your Time.

11 Likes

I don’t suggest ever using :Wait() and use an Event instead.
If you are using a while loop, you can use an alternative

game:GetService("RunService").Heartbeat:Wait()

because if you only use :Wait(), it is a bad way of yielding and can cause bad performance.
Also, to change values, I suggest you to use the Changed event rather than
while loops.

6 Likes

Use

while true do
    -- ...
end

It’s clearer.

It clearly indicates that the condition is true, and that the loop is infinite because the condition is constant. The only reason the while wait() do idiom works is because of a hack; a trick.

wait() returns two values. The first is the delta time (actual time waited). Lua uses that return value as the condition.

while wait(2) do

end

If this were not the case then the idiom wouldn’t even work. And if Roblox decides to make wait return false or nil, then your loops break. This is unlikely, but possible.

Also with the latter it is unclear on what the programmer is trying to say.

So, never call wait in the conditional part of the while loop. You are implicitly abusing the fact that wait returns a truthy value. Write clear and explicit code.

Case and point is that this question itself had to be asked, because the while wait() do idiom, is not immediately clear.

14 Likes

Basically, both technically do the same thing. While loops never stop, until you break them. While wait loops are conditions in which a condition has to be met before it executes. Also I don’t recommend using heartbeat as it runs every frame.

Technically both are similar but while true do will never end if it’s true or (something).


while wait(1.5) do 
     print("yes")
end

-- While loops

while true do
       local part = Instance.new("Part")
       part.Parent = workspace
       wait()
end
3 Likes

I am not sure if you understood OP’s question. OP is asking about the while wait() do idiom where you directly call wait in the conditional field of the loop.

while wait() do

end

while true do

end

cc @BasedKnowledge

4 Likes

oh sorry if you don’t undestand, basically, you dont use wait() in a loop
use events to yield it

3 Likes

The difference between a “while true do” and a 2while wait() do" is

A while true do --Loop with run when the the given argument is true

and in a while wait() do will run after a given ammount of time

–//While wait(Example)

while wait(1) do -- This function will run after every n(number) argument given.

print("Waited 1 Second")

end

–//While true(Example)

while true do -- This function will only run if the given (argument) is true.

wait() -- You add a wait to not break or crash you device as this functioon runs so fast

print("Runing because the argument given is true")

end
--Here is an example to see if the argument given is true to run the while loop

local Run = true --Run is true right here

---------------------

while Run do -- This checks if the argument run is true to then proceed with the function

wait()

print("Running")

end

--------------------------

–Now if we set run to False the loop will not run

local Run = true

while Run do

wait()

print("Running")

Run = false -- Now we made the run to be false this should stop the loop cause run is not true

end

Hope this helped you understand better :+1:

4 Likes

Unless you have something that yields the loop as it cycles, a while true do end loop could cause tremendous lag. This means that if you were to have an infinite, unyielding loop such as this:

while true do
end

Your game would be either really laggy or crash. Certainly would crash if it were on the client.

Now, to fix this, you would do:

while true do
     wait()
 end

And your computer would be able to handle this.

People usually include the wait() in the condition part of the statement to save lines. It’s faster to write. I don’t think it’s any harder to read, either.

So really you could either do:

while true do
    wait() -- could be game:GetService("RunService").Heartbeat:Wait(), like BasedKnowledge said
end
-- or
while wait() do
end
6 Likes

So you’re saying that while wait() do will run the loop inside whenever the conetnts of the while loop finish running?

You should avoid both, unless absolutely necessary.

while wait() do

end

This is bad, first of all because wait() is horrible in every context, it shouldn’t be used at all. Second of all, you are relying on a global function returning something, which you shouldn’t because that behaviour can always change. Instead, use task.wait():

while task.wait() do

end

Okay, that’s better, but you are still relying on task.wait() returning something. This, is better:

while true do
      task.wait()
end

“But where do I place the wait?” you may ask. Well, depending of the context, you might need to put it at the start of the function, but in general terms you should put it at the end. This is because the condition is first evaluated then the code is run, which means the evaluation will happen, and then the code will run. This is a very important issue that you may miss, in important logic you should never place task.wait() on top of the while loop!

And no, doing a while loop with task.wait() is not bad at all, because it is basically RunService.Heartbeat:Wait()

A difference between repeat and while loops:

repeat --> Code is first run

until false --> Then, the condition gets evaluated

while true do --> Condition is first checked
--> Then the code runs
end

I prefer

while true do
	--do stuff
	task.wait(0) -- wait 0 seconds (so the script doesn't time out or whatever)
end

as there is no way Roblox can break it, plus its simple and clear that its an infinite loop.

If I know i’ll need to break out of it, I do something like this:

local escape = false -- create escape bool
while not escape do -- if escape is equal to false
	--do stuff
	if someFunction() then
		escape = true -- break out of infinite loop
	end
	task.wait(0) -- wait 0 seconds (so the script doesn't time out or whatever)
end

I will place a better example later.

That would cause a time out, you might as well not even have a task.wait() in the first place, thankfully it doesnt actually wait 0 seconds.

break and continue exist

1 Like

I get this, however my peanut brain prefers an escape bool.

Weird, since when I do a task.wait(0) like that, it doesn’t cause a timeout. Unless roblox changed that within the last 24 hours since I’ve used Studio? Hopefully not, since a lot of my scripts do that :joy:

Thats why.

Ok. So the task.wait() being there at all is what prevents the script timeout?

Because task.wait has a minimum amount of time that it can wait

It also is never entirely perfect. I have a module that perfects this.