Should I ever use wait() function?

I have seen on some posts (can’t find which) that say the wait() function is unreliable and to use RunService instead.

What does this mean? Anyone mind giving me an example of how to use RunService instead?

Does this also mean I should never use the wait() function?

2 Likes

This is probably the most explanatory post and most helpful:

Just read around the examples and it will explain itself.

1 Like

Thanks for the reply :+1:, but what if you had a local script for a gun, and you needed to wait and halt the gun from shooting because your reloading? Like this:

cooldown = 2

fire()

wait(cooldown)

fire()

How would you use RunService to wait 2 seconds instead of using wait() function?

Yeah, I guess that would work.

Theoretically, lets say you had to use RunService to wait. How would you do it?

You could just use a heartbeat possibly, I’m not the best in RunService but something like

local RunService = game:GetService("RunService")
local rate = 2

RunService.Heartbeat:Connect(function(step)
local increment = rate * step
end)

this is just very simple.

2 Likes

Like this. Using RunService will get you waits accurate down to a single frame (1/60), which is usually way more than enough for most use cases. Here, it defaults to a frame if you don’t pass in an argument.

local rs = game:GetService(“RunService”)

local wait = function(t)
    t = t or rs.Heartbeat:Wait()
    local now = tick()
    repeat rs.Heartbeat:Wait() until tick() - now >= t
end

There are so many ways to do something like this, this is just one method of doing it

6 Likes

How performance heavy would this be compared to normal wait()?

This doesn’t do anything to performance. It does exactly what a wait() is supposed to do. It just yields the script. Just that with this, it’s more accurate, reliable, and you have precision down to a single frame (1/60 seconds), compared to wait()’s 1/30 second minimum.

3 Likes