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.
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.
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.
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).
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 .