oooo okay got it! thank you for the explanation!
“creating an infinite loop with no pauses basically means Roblox will never be able to proceed since it thinks your code is still running.”
but, can’t we have if statement that checks and breaks accordingly?
“it continues the loop even though you either disabled or destroy the script.”
So basically it will continue the loop even though you can’t see changes, which takes up memory?
It should be
Yes. It takes up untracked memory.
wait so if it still replicates the changes after the script is destroyed, how do we stop it?
Both wait at the same time.
The difference between using while true do wait() end
and while wait() do end
is that while true do
is optimized and doesn’t actually check the condition, it just repeats until a break happens. Internally, Luau won’t check if true is a truthful value, it gets “compiled” to bytecode which doesn’t take that into account since true is always true. It’s not that big of a deal though. Using RunService to handle things that “always happen” is a much better option, readability (to me), perfomance, and memory wise.
(This optimization above is true on Lua, therefore it should be valid in Luau as well.)
Source: luaK_goiftrue
under lcode.c
on Lua source code. (Got that source from While-Wait-Idiom)
A while true loop with a reachable break or return is not an infinite loop
Infinite loops are where the code never escapes the loop
ya I know.
That’s what I was tryna say lol
Please don’t ever use this, you’re relying on the fact that wait is going to return a truthy value, in this case, it’s the time it waited for. If roblox ever changes this, this will break.
while true do is more optimised than while wait() do.Why is wait() important? - #22 by LucasMZ_RBX
I doubt Roblox would remove the return from wait given how its useful to developers who want to make time-based animation code etc.
I can see why while true would be more optimized than while wait(). I am an expert a java programming as I literally take 5 hour long classes on saving memory and runtime lol. So, using my experience from that, I would kind of be able to tell why while true is more optimized than while wait()
Just going to leave this here… Not the biggest fan of while wait() do
either.
There’s no way they would change wait() to give off the completely opposite signal of what it already does. They are not going to change it as literally major of the platform relies on every way it functions. It’ll continue to work the way it does & they’ll completely take focus away from it.
Most coders recommend not substituting true
for wait.
Some say this is because one day wait may not return true.
You can look at some other reasons here:
As long as it doesn’t return nil or false, anything else will be considered true & it’s not directly printing true but actual values based on the wait, which way too many games take use of that it would be one of the last things Roblox want to change. But do they even have a reason to? I don’t think so.
Even then, the code looks worse and isn’t anymore performant. You’ve saved one line if anything. The literal sounding of the code is also more confusing.
while true do
wait(1)
end
“While true, wait one second”
Versus
while wait(1) do
end
“While waiting for one second, do what?”
This can and has confused people.
No way people are confusing it like that. No one is gonna make an entire loop just for a wait(), it’s obviously for all your code you want in the loop to run repeateadly.
Most of what I know is it just slows your code down, if you didn’t have wait() your code could slow down your game a lot depending on what the code is doing. Basically you should just try to put it into things you don’t want firing off every 0.001 second.
task.wait is basically a better version of wait because its more accurate.
That’s not the point, the point is the former looks cleaner while the latter just looks like you’re magically expecting wait to return something.
Anyway, lets stop debating.