Example:
Hi = true while Hi do print("Everything is awesome!") end
A while (condition) do
loop executes the code it a continuous loop as long as condition = true
.
Hi = true while Hi do print("Everything is awesome!") wait() end
This can be substituted with more concise code.
while wait() do print("Everything is awesome!") end
If you see while true do
, that intends to be executed forever, and if there is no wait, could crash a computer. Better practice for an endless while loop is while wait() do
. That accomplishes the same thing without crashing a computer as well as being more concise and readable. It should also be noted that if a script hits a while true do
loop that's outside of a coroutine it will not execute any code below it. For example,
while wait() do print("Everything is awesome!") end while wait() do print('Coding is awesome.') end --[[ endless output: Everything is awesome! 'Coding is awesome' is never printed --]]
It should also be noted that anything that doesn't explicitly results in false returns true. For instance,
Hi = 2+2 while Hi do print(Hi) wait() end --endless output: 4
Hi = 'Everything is awesome!' while Hi do print(Hi) wait() end --endless output: Everything is awesome!
As opposed to,
Hi = 4 < 3 while Hi do print(Hi) wait() end --no output
it endlessly repeats
print('Everything is awesome!')
to stop this you want to do
Hi = true while Hi do print("Everything is awesome!") wait(1) end