I really should avoid using wait() or wait(n)

I have read quite a few forum posts from people who strongly recommend to avoid using wait. Mainly claiming performance issues or some other things. But there are also people who say the opposite and that there is no problem with wait, which leaves me completely confused.

There is also the fact that roblox is constantly being updated and improved leaving many of the past opinions obsolete. For example, this is what it says in the documentation:

Roblox’s thread scheduler is exposed through the functions spawn(), wait(), and delay(). These actually work using coroutines.

My question is, with everything said above, should I really avoid using wait nowadays? And does the fact that wait works with corrutines really prevent me from having the problems people claim?

3 Likes

This post goes into much detail of where wait should and should not be used.

4 Likes

You should just avoid using wait() excessively. If you over use wait() (for example, having every part have a while wait() do loop) you can stop spawn(), delay(), and a bunch of other things from working.

I find that wait(n) is fine if you’re purposefully implementing a pause in the game (like a 15 second intermission or something). Wait() is really only problematic when you use it too much at one time, like during a while or for loop.

This is quite old. I would like something more up to date.

It’s still a perfectly fine up to date resource. Two years really isn’t that much time; Roblox hasn’t made any significant, visible changes to how their task scheduler works since then.

5 Likes

While it might be from 2019,

“Old but gold”

2 Likes

if roblox has not made changes, and according to this publication, roblox would be acting negligently by not fixing wait

There is no fixing wait.

However, there is fixing bad code which uses wait. :slight_smile:

1 Like

Alright, since you have been reading, you probably have noted that, this functions performance impact is almost null, pretty small, that would change nothing, many popular games still use this functions…
Using them will not be bad, and the performance impact will be as said before, almost null.

Though, if you really would like to not have any performance impact, there’s a bunch of ways you can achieve this, as making your own wait() function, or using runservice.

that post clearly gives alternatives to wait, because there is obviously something wrong with it. Why not just use wait if there is nothing to fix?

Nothing is wrong with wait. It’s how you use it that can potentially end up being a problem.

For instance, someone may do

local Players = game:GetService("Players")

local player = Players.LocalPlayer

wait()

local character = player.Character

It may work a couple times in Studio, but isn’t guaranteed to work 100% of the time. To 100% guarantee that character will always be correctly, you shouldn’t use wait. Instead

local Players = game:GetService("Players")

local player = Players.LocalPlayer

local character = player.Character or player.CharacterAdded:Wait()
1 Like

but in that post it says to avoid using wait. I’m just confused. Please don’t misunderstand me.

I don’t understand why the two codes are different (assuming there is nothing wrong with wait).

in the first code maybe you wanted to do this

while not player.Character then 
	wait()
end

In my first example, I use wait hoping that the player’s Character will be ready by the time the next line in the code runs.

To guarantee that the character actually exists, I use the Wait method of CharacterAdded.

Also no, I meant to use the code that I did in example 1 as an… example :stuck_out_tongue:

Basically whenever you use wait, ask yourself if there’s a better alternative that can insure the result you want to accomplish.

I understand your point. thank you for being so patient with me.

1 Like

Absolutely. :slight_smile:

Good luck!