Common causes for lengthened wait() time?

My game relies on the wait() function quite heavily, but for some reason the delay time to wait() can be highly inaccurate. Usually this inaccuracy is small (0.03 sec), but in some cases it can get as long as 3 seconds! This is a huge problem for my game when it happens, since it basically makes the game unplayable.

I can use alternatives to wait(), but I would rather try to fix it before trying to avoid it completely.

This topic has been asked before, but people only answered with why it happens internally, and not the general causes for it.

1 Like

Wait will wait as short as it possibly can, at a minimum of the time you tell it to wait for. But, your question has already been answered. How long does wait() wait for?

It can happen because of lag, and a multitude of things. It’s extremely difficult to narrow down to one thing.

Not exactly answering, but wait() is really, really bad

Using wait() can result in a lot of inaccurate effects, especially if the game relies on a lot of performance

Alternatively, you can use a RunService event which would be way faster than just using wait()

2 Likes

What I use, courtesy of DanzLua.

function safeWait(seconds)
	local waitTo = tick() + seconds;
	while (tick() < waitTo) do
		game:GetService("RunService").Heartbeat:Wait()
	end
end
1 Like

Can there be any performance issues with this wait function? since it updates every frame.

The link posted above gives a general idea for why wait() can be terrible in practice, but it doesn’t exactly explain why. The reason wait() has different length times is because there is no guarantee for when it will resume. If you are unlucky and need a ton of tasks to be regularly resumed, you can go over the per frame quota and the wait() may take a second rather than the ~0.03 seconds.

This is also why I’m an advocate for yielding on events using the provided :Wait() command. It is much better practice and will give you the results you want. However, if you need a custom accurate wait function then you should use a timer that runs on each frame like Heartbeat.

Not performance issues that are a result of the function, this will work fine.

Similar posts:

Yep no major performance issues with that wait.

The explanation is that Runservice is newer and simply runs faster in 60 Hz therefore is more likely to resume paused code faster and have less delay than wait(). This delay with resuming code causes the lag (for others new to the issue it’s shown here in this video by clone trooper).

Evaera

spawn , delay , and wait are all artifacts from Roblox’s 30hz pipeline, and they don’t resume at a consistent rate. In fact, there is no guarantee that they will ever resume at all .

1 Like

Well, it might be better to just switch to the Heartbeat wait function then.