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

which while loop should i use?

Asked by 5 years ago

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
0
use while true do, instead of while wait(1) do User#23365 30 — 5y
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 Vulkarin 581 — 5y
0
^^^  User#23365 30 — 5y
View all comments (6 more)
0
That doc was made by a moderator User#21908 42 — 5y
0
a staff member I mean User#21908 42 — 5y
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 User#22604 1 — 5y
0
I would use while wait() do because I don't wanna put while true do and forget about the wait() greatneil80 2647 — 5y
0
bad greatneil80 User#19524 175 — 5y

2 answers

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

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.

Ad
Log in to vote
-3
Answered by
OfcPedroo 396 Moderation Voter
5 years ago
Edited 5 years ago
  • 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

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! User#19524 175 — 5y
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... OfcPedroo 396 — 5y
0
Shorter code argument is a bad argument as well, and saying while true do is laggier is all falseeeeeeeeeeeeeeeeeee User#19524 175 — 5y
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. OfcPedroo 396 — 5y
View all comments (2 more)
0
My downvote will stay. This is too much misinformation. User#19524 175 — 5y
1
`unlike while true do` You're giving it true, y'know, the Boolean that returns when a value's truthy? TheeDeathCaster 2368 — 5y

Answer this question