Which one is less efficient between while wait() do and while true do wait()

I don’t want you guys to tell me what is better then wait() all I want to know is difference in efficiency between while wait() do and while true do wait()

thanks for reading, doryu

2 Likes
while wait() do

end

wait() returns a number. As long as it returns the number, it will work. However, if roblox decides to update wait(), and make it no longer return anything, your code will break.

Using the other method will make ensure your code does not break due to roblox updates.

Not much of a difference, however I would not use while true do loops with wait().

1 Like

so while true do wait() is safer to use?
also yes ik I shouldn’t use it

1 Like

Yes it is more safe because it wont break if roblox updates wait().

ok just making sure, thanks for your response

1 Like

For backwards compatibility it is unlikely we would stop returning the total amount of time elapsed waiting, therefore it is safe to use:

while wait() do

end

As for efficiency, it doesn’t really matter. Use whichever one you prefer.

6 Likes

thanks this is very helpful, appreciate it

1 Like

I also highly recommend using Custom Wait because built-in wait is not everytime the best solution for yielding.

Custom Wait Open sourced

B-but that’s an extra TEST opcode!!
image

On a sidenote, the OP should read this: The While-Wait-Do Idiom, by cntkillme: Addressing while wait() loops
the TL;DR is that you shouldn’t use while wait() do or while true do wait() in the first place, but rather have event-driven code. If you need to update something every frame, use RenderStepped, Stepped or Heartbeat. There are practically no cases where you actually need such a loop.

3 Likes

it’s actually a test + an extra jmp since the code needs to exit the loop in case the condition is false

well if youre going down to specifics, then theres also CALL and GETTABUP

I’m not saying you said this at all, but you do know those aren’t loops unless add a RS or S or HB:Wait() in a repeat or while loop

1 Like

those are 5.2 opcodes, not 5.1 (which is what luau is based on because of backwards compatible environment functionality)

1 Like

CALL is in 5.1 for obvious reasons but I forgot about GETTABUP, yea

1 Like

It’s a loop in abstract form, I’m not talking about actual loops here… RenderStepped and the other methods are continually repeated, and even though there is no condition you’re just nitpicking here IMO - I think it’s clear what I was implying… If you want a more specific reply, then it is an event, yes. It’s function has the same effect as a while wait() do block though, albeit with differing update rates.

not exactly what I was meaning to say, if you do
game:GetService(“RunService”).HeartBeat:Connect(function()
end)

this runs every frame(a specific part of the frame ik that) and it won’t wait for the code inside to finish to run again

a loop waits for the code inside to finish in order to run again

that’s the difference
EDIT: I can’t seem to put the code into format sorry

1 Like