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???
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.
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.
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"
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?