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

which while loop should i use?

so i'm scripting a day cicle script but which code should i use?

while true do
	wait(1)
	game.Lighting:SetMinutesAfterMidnight(game.Lighting:GetMinutesAfterMidnight()+1)
end

OR

while wait(1) do
	game.Lighting:SetMinutesAfterMidnight(game.Lighting:GetMinutesAfterMidnight()+1)
end

10 comments

0

use while true do, instead of while wait(1) do

2

wait will not return False, and it's used by a ton of commercial software but this entire website insists to use while true...don't really matter though

0

^^^ 

0

That doc was made by a moderator

0

a staff member I mean

1

it's up to opinion, you can even do while wait(1) and true do which obviously doesn't matter for this but is useful when you have a condition

0

I would use while wait() do because I don't wanna put while true do and forget about the wait()

0

bad greatneil80

2 answers

✓ Accepted answer

#It's a Hack.

I don't mean a hack as in exploits or whatever, but a hack is in a trick. while wait() do abuses the fact that wait returns a number (it actually returns two), the first one corresponding to the time Roblox yielded the thread for. Since in Lua, truthy values are anything that is not false or nil, and numbers are not false or nil, the number returned is abused as a condition. If wait were to not return anything, or if it returned a falsey value, you could not use it as a condition. It's somehow dependent on wait returning a truthy value.

#Readability.

You should use

while true do
    wait(t)
    -- ...
end

It's clearer. It's also more logical, it tells you that the loop's condition is true, and that it does not necessarily have a terminating condition. It makes zero sence to use a number as a condition. This can be interpreted as do this forever, as the loop's condition is always true and therefore is an infinite loop.

Use while true do wait(t) ... end, for these reasons.

  • while wait() do doesn't return false, unlike while true do (obviously), which could be an advantage sometimes;

  • while wait(1) do is a simplified way of writing while true do with a wait(1) inside;

  • while true do, without any waits or other timeout functions or statements inside, can sometimes break the game or cause some lag spikes;

  • while wait() do includes less code than the second choice, which will be more readable.

(Personally) I never use while true do, and also because of the points I just gave, I don't recommend using it, and I always find other ways around, which are usually better. So, in this situation, and if you have no other conditions, I would use while wait(1) do, but it's really up to you.

If my answer solved your problem, please mark as the solution. :P

6 comments

0

-1 for saying "while wait() do includes less code than the second choice, which will be more readable and faster." Shortercode is not necessarily faster code!

0

Ok, jeez, you didn't need to -1. You could just have told me, and I would edit, like how I'm going to right now. Thought, perhaps not on Lua, but I'm pretty sure on some coding languages, specially if it is a quite bigger code, it can have performance differences...

0

Shorter code argument is a bad argument as well, and saying while true do is laggier is all falseeeeeeeeeeeeeeeeeee

0

Nope, it's not false. while true do can lag stuff if you don't set any wait() inside. It crashes my ROBLOX Studio just by itself -.- And yes, shorter code is usually more readable, that's something correct.

0

My downvote will stay. This is too much misinformation.

1

unlike while true do You're giving it true, y'know, the Boolean that returns when a value's truthy?

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