I am new in the script word, so, whats the difference between "while true do" and "while wait() do"?
What's the difference between "while true do" and "while wait() do"?
3 answers
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.
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.
###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.
This discussion is preserved as it was. New answers, comments, and votes are closed.
