Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What does the while do statement do?

Example:

Hi = true
while Hi do
print("Everything is awesome!")
end

2 answers

✓ Accepted answer

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

1 comment

0

This is good but edit this and explan more than the other one and i'll accept yours!

This discussion is preserved as it was. New answers, comments, and votes are closed.