Using while wait() - Does is repeat?

Will this code repeat itself until the time is reached or will it execute the code during the wait time once?

while wait(x) do
	-- Some code
end

It does repeat, but there will be a wait of x each time it runs

A while loop will always run as long its condition is true. When adding a wait, or more properly, a task.wait(n) inside the condition, the loop will continue to loop forever, but between each loop it will wait the amount of time stated inside the condition. For example:

This is what I like to call the “While formula”:

while task.wait(n) do
    -- // Execute code 
end

You can basically just read it as While true, wait X amount of seconds, and then do:

Here’s an example:

while task.wait(10) do -- // While true, wait 10 seconds and do
    print(“Hi!”) -- // Prints out every 10 seconds
end

If you want to execute a loop between a specific time, use a for loop instead!

1 Like

to add on to what other people have said, instead of writing

while wait() do
--code
end

write

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

the reasons for it being:

  • the second example saves a few instructions by moving the wait call outside of the condition. while true do loops have very minor optimizations
  • the second example uses task.wait instead of wait. task.wait is newer and waits for an amount of time more precisely than wait can

while loops only take 1 valid argument. True or false (well, truthy or not).

When running a loop, it will solve whatever is in the conditional part until it resolves to a truthy or falsy.

So if you were to do something like

while 5 == 5 do
    print("was truthy")
end

When it hits the while loop, the loop resolves the conditional. This means it runs the stuff inside it.
5==5 is true so this resolves to true.
To see this more clearly, you can set a variable equal to a value

local isTruthy = 5 == 5
print(isTruthy)

That will print true because the == is just returning a true or false if it is true and doesn’t need to be in an while loop.

However, a while loop doesn’t just look for true, it actually only cares that it’s not false or nil. If it resolves to anything that isn’t false or nil it’s considered truthy which the while loop treats as true. So while 1 do would run indefinitally because 1 is true.

a while loop will run any functions in the conditional as a part of collapsing it. game.Workspace:FindFirstChild(“Part”) will return part which is a truthy value and since the conditional resolved to truthy, it will run.

wait() does return something. It returns how long it actually waited for. So a wait() will wait for the amount of time it should and run at it’s next opportunity returning the actual amount of time that has passed. Since this is a truthy value (being a number, not nil or false) the while loop then considers it as true.

So to sum up, the while loop will ask wait() to return a value. Wait will wait the amount of time it should, then return a number. Since wait returned something truthy the loop will run once again.

1 Like

It’s more optimal to use task.wait because the normal wait is deprecated(I assume it was recommended to you by ChatGPT, it has a tendency to use it). Your code will repeat forever unless the thread in which it runs in is terminated. This can happen through multiple ways, some including:

  1. The script object gets destroyed/disabled
  2. This is inside a task that you terminate through task.cancel or stop through task.yield
  3. An error occurs in one of the loop iterations, causing the entire thread to die(unless you pcall the code that might error)

Also a forever loop can be stopped within the loop itself through the use of the break statement, for example:

local i = 1
while task.wait(0.5) do
	print(i)
	i += 1
	if i > 25 then break end --the loop will stop when i passes 25
end

A loop yields the thread until it’s fully completed before running the code underneath it, so if under a loop you add something like print("Hello World!") but the loop runs forever, it will never print. That’s why we sometimes need to make separate threads for certain loops that we don’t want to yield any code(event connections use different threads by default, so if on an event a forever loop runs, it won’t yield code that is outside and under that event connection).

The reason while task.wait(x) do loops work is because on each loop iteration the loop condition(in this case task.wait(x)) is checked. In order for it to be checked the task.wait function is called, that returns the total actual amount of time it took for it to run(an approximation of x) after around x seconds pass(so it yields the code during that). Since what it returns is a real positive number, it always gets evaluated to true(because not not num is always true) and the loop runs forever.

To answer your question directly, yes, it will just repeat forever.

A while loop looks something like this:

while <statement> do
    ... stuff ...
end

If the statement (something like x == 5) turns out to be true, the while loop repeats.

In languages like Luau, the statement in the while expression does not have to evaluate to a boolean. This means that this code:

while 7 do
    ... stuff ...
end

Would run forever. This is because 7 is considered to be a “truthy” value. Any non-zero number is truthy.

The final piece of the puzzle is that wait() returns a number representing the amount of time that the function actually waited.

So this code:

while wait() do
   ... stuff ...
end

Would also run forever, because wait will always return a value considered to be truthy.

Roblox scripters adopted this pattern because it was more compact than doing something like this:

while true do
  wait()
  ... stuff ...
end

However, I think it’s bad practice. (You should also use task.wait instead, like other people have pointed out.)

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.