Alternatives to while wait(0) do?

When using while loops, it requires a yield or else it prints an error of a script timeout. To avoid this, some use a hacky way of bypassing the yield requirement by using wait(0). This is the same as not adding it and I don’t see a difference. Is the wait time inaccurate and would using a custom wait fix this hacky way?

2 Likes

It still yields if you use while wait() do .

Would rather leave this bad practice for something better when it gives no benefit at all.

1 Like

Please try to avoid it:

For the first part:

For the second part:


Alternatives involve for the former: RunService events and for the latter: proper usage of while loops and events.

2 Likes

Infinite loops need to yield in some form. Are you sure you can’t listen for an event here?

2 Likes

I agree that this is bad practice and developers should have a non-hacky way of bypassing this.

Similar to what @StrategicPlayZ said, it’s not very good practice to use while wait() do

I honestly couldn’t tell you why, but a lot of people much smarter than me have all said that this is a little better:

while true do
    wait()
end

Additionally, to answer your question in full, no I don’t really suppose there’s a better way to have an infinite loop on the server.

1 Like

There is no non-hacky way of bypassing, it isn’t even possible. All infinite loops need to yield or they will crash. For a smaller wait you can do RunService.Heartbeat.

1 Like

Would using os.time() as a replacement for wait help in any way?

That function does not yield, it returns time(numbers).

1 Like

Sometimes I rather use while loops. I know I can disconnect events but still. While loops are used by lots of developers and using hacky ways to bypass this is bad practice.

The only way you will be able to “bypass” yielding in a loop is by having some code in it which yields, for example an HttpRequest etc.

But still you will need to yield an infinite loop using some way anyway, whether that be by wait() or a yielding function.

1 Like

On a related note, if you’re not using a server script, but instead a localscript, here’s a way cooler and faster way to make an infinite loop:

game:GetService("RunService").RenderStepped:Connect(function(Delta)
    print("This will print every single frame of your game!") -- Only works in local scripts though :(
end)

I would check out RunService, there are 2 other kinds of loops like this, and though they all do really the same thing, it’s more about when they do it.

1 Like

function waitReplacement()
local start = os.time()

return (os.time() - start) > 2
end

I was thinking of using this as a replacement. Would it work better? (Sorry for no formatting, I’m on mobile right now.)

I know about runservice, but the use of the break function is easier for me.

Well I have two main points.

  • I see what you’re trying to do, and to that I say no please do not try this. Roblox is single threaded so this will just cause FPS lag.

  • This code doesn’t even work lol, you would need, yet again, a while loop.

1 Like

RunService.Stepped:Wait() is an alternative though. It worked fine when I used it in a while loop that checked for a condition before leaving the scope.

2 Likes

My thoughts exactly, if you want to stay in this breakable loop formula, then this is what you’re looking for, assuming this is local.

1 Like

I guess you could try using Heartbeat or Stepped or RenderStepped.

1 Like

Actually, for a better alternative of most cases. You should utilize signals that indicate something has changed. Once that property changes, fire a function from that point.

1 Like

If they have the run service functions, then shouldn’t while loops be deprecated by now? If they provide a use that run service doesn’t have then this issue still needs to be addressed. @wastemanty23 @RepValor