Wait(), will it create lag? and how can I replace it?

  1. So Im basically trying to reduce lag and server slowness reducing while loops and waits.

  2. I have looked for the solution and wait alternative but none of them seem to work, have I done something wrong? Help would be appreciated

So, If you can help me out with this and pretty much show me how you would use waits alternatives then that would be really appreciated, Here is a quick example of code which I would use a wait for.


local stage = game.ReplicatedStorage.Folder.Stage
local StageClone = stage:Clone
wait(1)
StageClone.Position = Vector3.new("blablabla")
1 Like

What exactly are you trying to do? You shouldn’t have to put a wait before changing the position.

1 Like

Im just giving an simple example of where a wait could be used, this script is not being used in my game I just want to know how you would replace a wait with something more efficiant and that creates less lag.
If my code was that then yeah I would probably not use a wait lol

1 Like

Wait() inherently doesn’t cause lag, but the way it’s been implemented makes it inconsistent. If you need something perfectly timed every time, with maximum efficiency, use this resource:

2 Likes

Thanks! Yep, I need something more accurate then wait that wouldnt break the game if the fps drops because wait actually can go over the time requested to wait depending on the frames per second/game performance.

Okay, I see. Though I think wait(1) is already efficient. What isn’t efficient is wait() with very small numbers in a loop. An alternative to this (on the client) could be RunService.Stepped:Wait()

1 Like

That is true. For client-sided purproses, you can use RunService.RenderStepped:Wait() instead of the stock wait() function. Wait() can be used for things that don’t need to be perfect, but it’s definitely questionable in its reliability.

2 Likes

The module was created because Roblox’s wait scales pretty badly - if you use like 30 waits you’re fine lol. My wait module is for semi-extreme cases where you NEED that many yields at once, but 99.9% of the times you won’t need that many yields. If you do, you should probably think it all over since you’re almost definitely doing something wrong lol.

1 Like

so does that mean I can have a script that uses tons of waits? or will this affect something. I actually have a script that has like 8 waits and several other scripts which would probably result in around 40 waits running. the waits will usally be short ones. not too short tho like wait(1),

In my case Im making a simulator, people buy stuff which will not requite waits in my scripting experience.

I would probably have like at the maximum 20 waits running in the SAME time.

will this create any problems? will the game start to slow down in any ways?

To my knowledge wait() won’t slow down the game at all as you are essentially telling the scripts to sleep and wait for an amount of seconds. You shouldn’t get any problems from having 20 waits.

1 Like

Alright sounds good! Thanks for your answers, I’ll try it out without wait the only thing Im scared of is people joining the game and the game crashing, other than that nothing would happen “hopefully”.
So yeah this is pretty much all I got to say, i hope I dont get any problems.

1 Like

there is still one thing bugging me, waits drop and are really unstable, when the fps goes down or drops the wait amount goes crazy and you end up waiting 5 seconds instead of 1.

wait() is inconsistent and can actually yield for much longer than you specify.

It can be heavily affected by lag.

It almost certainly won’t cause lag by itself, though. It’s a very simple function that yields the current thread and resumes it at some point in the future.

1 Like

yes but how would I replace it? this is really what I need to know.

If you’re trying to accomplish that, there’s no need. It’s not that slow.

But if you’re referring to the inconsistency problem, there’s lots of ways depending on what exactly you’re trying to do. A lot of the time it involves binding to Stepped or RenderStepped.

For example, if you’re making a counter that ticks up once every second and you found it to be inaccurate, you could do something like this:

-- old way
local x = 0
while wait(1) do
    x += 1
    print(x)
end

-- new way
local startTime = os.time() -- also might want e.g. tick() or time() depending on your use case
local x = 0
game:GetService(“RunService”).Stepped:Connect(function()
    local totalTimeElapsed = os.time() - startTime
    if (totalTimeElapsed >= x + 1) then
        x = math.floor(totalTimeElapsed)
        print(x)
    end
end)

But again, that’s not a one-size-fits-all solution. This timer would skip numbers e.g. if (for some reason) a frame took longer than two seconds. Maybe that’s not what you want. Look at your application and tailor the solution appropriately.

Forgive any typos etc. I’m on mobile.

1 Like

Thanks I’ll try it out and I will tell you how it works on my side, If it does end up working Im going to mark you as solution, I’ll get back to you as soon as possible

Also, maybe to avoid the xy problem, you could post what your actual use case is instead of this general question about wait, which doesn’t have a clear answer.

1 Like