Pointers to reduce lag in your game

Doing wait(.01) won’t have a different result than wait().

Every “wait for certain amount of seconds” function works by checking every frame, and sees if those seconds have passed.

With Roblox’s wait, it checks that ~0.03 (1 / 30) seconds, which on 60 FPS means that it’s checking that it’s 2x less than every frame, and it gets worse as the more frames you’re able to render, as that difference gets more impactful, and it will lag behind more and more.

There are apparently some other issues with wait just being glitchy, weird apparently too, not only that.

Anyhow, most of the time you don’t NEED to use wait.

Is it a question of changing / doing something everytime?

  • Use RunService events like RenderStepped, Stepped, Heartbeat and connect a function which does what you want to it.

Is it a question of having to check every time to see if something is true / if there’s something somewhere?

That’s called polling, which is like asking the computer every time “Are we there yet?”

  • You can use a custom Signal module (to create custom events) which allows you to use :Wait() which just yields the current thread until that event is fired, for example.

If you ABSOLUTELY need to use a Wait function, then this is at least decent compared to Roblox’s.

local RunService = game:GetService("RunService")

local function Wait(n)
    n = typeof(n) == 'number' and n or 0

    local spent = 0
    repeat
        spent += RunService.Heartbeat:Wait()
    until spent >= n

    return spent, os.clock()
end
3 Likes