Replacing wait()

After browsing the devforum for a while, I have learned that wait() is unstable and I should replace it with something like Heartbeat:Wait(). But how do I exacly replace it? Can I use this code for replacing wait() in my scripts?

function wait(ins)
	return game:GetService("RunService").Heartbeat:Wait(ins)
end

You could use a custom implementation of yours, although I don’t know if that code will work. What I would recommend is using the Custom Wait from this DevForum post which I used before and would recommend it

It isn’t difficult to use either, just get the source code form the post, make a modulescript, do ctrl+a and delete the code automatically added and paste it in

1 Like

I know there are many people on the forum telling bad experiences and empirically demonstrating why using wait is a bad idea, but no one has been able to give a definitive explanation as to why. Mainly because there is hardly any documentation about it and Roblox has never spoken for or against those ideas.

wait’s official documentation states:

The delay will have a minimum duration of 29 milliseconds

For some reason tasks that are created with wait have lower priority so they may take a little longer (or much longer) to resume.

But the solution to this is not a “Never use wait”. Continuing with the documentation:

but this minimum may be higher depending on the target framerate

This affects not only wait but any alternative.

and various throttling conditions.

This is the important part. “throttling conditions” refers to the fact that if we schedule many tasks (many coroutines, events, …), the tasks scheduled with wait will take longer to be solved.

The solution is obvious. Schedule as few tasks as possible in a given time, especially when we are using wait.

Of course, there are situations where using the alternatives is technically correct (for example in precise counters), but to say never use wait, not only has no technical basis, but it is not an intelligent solution.

It is very unlikely that roblox has made a buggy tool with critical flaws, so there is nothing wrong with using wait.

2 Likes

You should not be using yielding functions such as wait unless you have to
For example, if you want to make a countdown timer, you would have to use it

If you want to do a task where there needs to be a certain instance in the workspace, you will not be using wait, but WaitForChild.

If you want to tween an instance and wait until it completes, you will not be doing wait(time), but tween.Completed:wait()

1 Like