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
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.
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