Why is wait() important?

Hello, everyone.

The question is simple. Why is wait() important? I see tutorials and people putting wait in every iteration of loops.



If your loop has a large amount of iterations (or maybe infinite) then you will cause lua to hang because too much is happening approximately at once.

Also it is suggested to use task.wait() over wait() now.


While Loops

One of the most common uses is in while loops.
Most people would use this format when making a while loop.

while true do
    wait(--[[number here]])
        Code Here

but an easy way to simplify that is by doing this.

while wait(--[[number here]]) do
        Code Here

More experienced programmers suggest while wait(--[[number here]]) do, because this shortens the development process. but you will see while true do in a lot of YouTube tutorials, and that format is mostly used to understand the script more for beginners, being one of the main reasons it is used in YouTube tutorials.

1 Like

but, doing so will make fast loops slower.
What is task.wait()?

gotcha. Thanks for the suggestion!

Making loops go too fast can crash your game if it lasts too long, if you only loop a certain amount of times like a for loop then its fine, but a while loop should have a task.wait().

1 Like

It’s essentially the same function as wait(), but should be used because wait() is being deprecated soon.

Keep in mind when doing it the second way that it will wait BEFORE running your code versus the first one that waits AFTER your code executes.

but what is task.wait()? What is its actual purpose compared to wait()?

yup yup that’s true. But shouldn’t be that long of a wait most cases

really? Well okay then. But they functional completely normally?

wait() is being deprecated in the terms that Roblox don’t want you to use it. But actually removing support for it will break way too many games on the platform, so it will keep existing & functioning. The problem is wait() throttles a lot & isn’t very accurate. task.wait() is essentially basing directly on the heartbeat which is ran each frame. So you should safely expect it to be done waiting on the exact frame it’s due, which wait() often doesn’t.
It’s just much more reliable.

1 Like

For this case no, but for longer waits (i.e., 1 second+)

task.wait() is what you want to use. It’s a lot more accurate than just using wait()

The main reason for task.wait (and its deprecated friend, wait) is to pause your up for a short period of time.

The time, with no argument defined is 1 resumption cycle (generally 1 frame since Roblox cycles are 60hz). It can, however be modified to wait as long as you want it to.

You should be using it in an infinite loop (such as while true do) to ensure your game doesn’t freeze. Roblox needs to know it can pass onto the next frame of operations knowing your code has completed, creating an infinite loop with no pauses basically means Roblox will never be able to proceed since it thinks your code is still running.

I can give you a more technical rundown but you’re a beginner so I dont really want to give you too many technical terms.

1 Like

There is one down-side about task.wait(), it continues the loop even though you either disabled or destroy the script.

Although wait() is not encouraged to be use, wait() corrects this flaw.

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.