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

What's the difference between "while true do" and "while wait() do"?

Asked by 5 years ago

I am new in the script word, so, whats the difference between "while true do" and "while wait() do"?

3 answers

Log in to vote
1
Answered by 5 years ago

while true do is a loop that runs an infinite amount of times

while true do
print("Hello World!")
end

^ This will crash your studio because it has no wait() ^

while true do
wait()
print("Hello World!")
end

^ This will not because it has something that prevents it from running an infinite amount of times in less than it can handle

while wait() do is like while true do but it already has a wait in it

while wait() do
print("Hello World!")
end

This will do the same thing that the previous code block shown will do but written down slightly shorter.

Ad
Log in to vote
0
Answered by
INOOBE_YT 387 Moderation Voter
5 years ago

They are both the same thing. the while and do are always there, and the part in the middle can be changed. People like to write while wait() do because if they do not put the wait(), the script will crash. Other people like to do while true do and add a wait() somewhere inside the block. Overall, no difference.

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

while CONDITION do runs code whilst the condition specified is true, breaks if it becomes false.

while wait() do

end

That code above only runs because one of the things that the wait function returns is the time waited, and because that value is a truthy value, the code runs indefinitely.

while true do
    wait() -- without a wait, the script breaks 
end

You should instead use true, as this is more expressive and it's slightly faster than wait(). For these reasons, use true as the condition.

Answer this question