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.
There might be that flaw, but wait() doesnât really wait the exact time you told it to. Use task.wait() instead.
It is good to knowledge that problem though, but I still donât see that as a reason to stop using task.wait(). Unless you run into that problem, which is rare
Wait() is important because you might need to make the script wait a few seconds.
if ur using a while loop, u need it for the script to not crash
Is there an announcement on this? Feels like a huge change.