What do you all prefer? I’m assuming the behavior of waiting right before running other code within the loop.
while task.wait() do
-- code
end
or
while true do
task.wait()
-- code
end
What do you all prefer? I’m assuming the behavior of waiting right before running other code within the loop.
while task.wait() do
-- code
end
or
while true do
task.wait()
-- code
end
while task.wait() do
-- code
end
is better
Ok, why? I remember hearing about using while true do as a better practice a whiiiiiiiiiiiile ago (before Luau). I’m confronting this potentially bad habit of mine
Also, the while true do block uses one less bytecode operation per iteration, right? As an aside from my original post: If you use while true do, you have more access to where the yield happens (before or after the rest of the code in the loop), right?
i just think its better because its cleaner. Having true there is redundant if youre going to just use a wait anyways
I know like nothing about bytecode or how to read it, i have no idea
From what I’ve heard, the while true do end paradigm gets optimized, and I have not heard of luau optimizing the other into the first one?
I don’t know how to read bytecode, but from just guessing, the first one seems to explicitly check the return of task.wait(), and if it’s false, jump past the loop, which the second one never checks
I’ve gotten used to just write while true do end, and not put the task.wait() inside. The difference it makes is not big. Perhaps cleaner, makes it more obvious you’ve got an infinite loop. Takes one more line
You can also play with where the task.wait() is. If it’s in the conditional block, it’ll always be evaluated right at the beginning, which might not be desirable
Ok so the solution you marked is right that it looks ugly. But that’s not it, there are problems with it too. It’s not just a bad habit.
You are yielding before any of your code inside the loop actually runs. I’m on phone so I can’t write up the code properly but look:
while true do
-- Code will run first ✅
task.wait() -- Yields AFTER code is finished ✅
end
while task.wait() do -- Script will yield before even running the code ❌
-- Delayed code runs ❌
end
Your code is being delayed before it even runs.. sometimes you might make the wait time longer for whatever reason. Which will make a longer delay.
Even if you keep it just as a small task.wait() sometimes a server can be so laggy and slow because of the amount of stress it has on it for whatever reason (expensive tasks eg excessive physics computing) To the point where even small delays can feel huge
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.