Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
4

while wait() do or while true do? [closed]

Asked by 5 years ago

So there are some while loops ( while true and while wait ) but... whats the difference?

so i know while true is always working and never stops but you need to put a wait(time) in it but while wait() do you put while wait(amount of time you have to wait between each event i think) do so i know they both work non - stop but

WHEN SHOULD YOU USE A WHILE TRUE DO OR A WHILE WAIT DO???

0
Some people don't like using the number returned by wait() as the condition for their while loops, but it really doesn't matter. theCJarmy7 1293 — 5y
0
By the way you can stop any sort of loop with break LateralLace 297 — 5y

Locked by User#19524

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

4 answers

Log in to vote
7
Answered by
Avigant 2374 Moderation Voter Community Moderator
5 years ago

while condition do runs while condition is not falsey (false or nil in Lua).

while wait() do

end

This code only runs because wait() happens to return the time it actually waited as its first return value, and since that value is not false or nil, and never will be, it will run indefinitely.

Instead, you should prefer this:

while true do
    wait()
end

This much better describes the behavior you actually want, and is more expressive. For this reason, use this approach.

Case and point is that this question itself had to be asked, because it's not immediately clear.

Ad
Log in to vote
1
Answered by
zblox164 531 Moderation Voter
5 years ago

They are the same. The only difference is while wait() do shortens your code. How? While loops need a wait function to work. Placing that wait function at the top of your code removes a line from your code. Thats the difference.

0
Just realized someone already said the same thing as me in an answer. LOL zblox164 531 — 5y
Log in to vote
0
Answered by
Rawblocky 217 Moderation Voter
5 years ago
Edited 5 years ago

while do loops

While do loops are used for endless loops, or even not endless loops.

while true do means that it will endlessly loop, however, it needs a wait() or the script will break while wait() do doesn't form any error, since their are waits

"Why are there while do loops?"

while do loops are for something like, that this for example: kicking all of the players out of the game

while game.Players:FindFirstChild('Player') do
game.Players:FindFirstChild('Player'):Kick('Removed')
end

It will kick all of the players until there are none.

This is the same thing as while true do Expect, that "true" can't change, so it's like keep on looping until true is false, which isn't happening.

While wait() do is basically it waits 0 seconds, then it loops again. This is basically what it is.

"When should you use it" If you want to make a loop start immediately, then use "while true do", but put a wait somewhere in the line, or else it will break

If you want it to, lets say wait a couple of seconds then it loops again, or make it not immediately start, use "while wait() do"

0
Wait() alone waits around 0.02 seconds, not 0 seconds. User#19524 175 — 5y
0
And he was asking the difference between while true do alone, and while wait() do, not what “true” was. User#19524 175 — 5y
0
Did you mean FindFirstChildOfClass? theCJarmy7 1293 — 5y
0
Yea User#19524 175 — 5y
Log in to vote
0
Answered by
Aimarekin 345 Moderation Voter
5 years ago

Let's shorten this:

They're same.