# Which is better performance wise, wait() or wait(x)?

Hey all,

just a quick question that popped in my mind, imagine a while loop that only runs wait.
Which would cause less latency, wait() or for example wait(1)?

I know that the while loop with wait() would run many more times, but for the other case the process would also have to count how much time until 1 second has passed, right?

In case you need to know why I’m asking, I’m trying to optimize this AI script that tells an NPC what to do and when its HP goes down to 0, it basically enters a while loop with wait() that waits until its HP gets back up to a non null number.

When wait() is called with no parameter, it waits for about 1/30th of a second, which is the minimum amount of time that wait() can yield.

Although the wait function is guaranteed to yield for at least the inputted amount of time, you shouldn’t count on it waiting for that exact amount of time. Luckily, the function returns the actual time yielded, which you can use to account for that.

If you plan on using wait() in while loops it might be a better idea to use `RunService.Heartbeat:Wait()` instead.

The `Heartbeat` event fires on a variable frequency so there is always a chance of some noise, but under ideal conditions it happens about 60 times per second, so if you were to implement a custom wait function using Heartbeat, it would have a minimum of about 1/60th of a second rather than the wait() function’s 1/30th of a second.

You could also bind your loop logic to the event as an anonymous function, removing the need for an infinite loop altogether.

``````local rate = 10 -- 10 HP/sec
while (HP < 100) do
HP = math.min(100, HP + rate / 30)
wait()
end
``````

In this example, an assumption is made that the loop will run at a frequency of 30hz. This code will work, but the timing may not be as precise as you expect.

``````local rate = 10 -- HP/sec
while (HP < 100) do
local dt = wait()
HP = math.min(100, HP + rate * dt)
end
``````

This example accounts for the amount of time yielded when the wait() function was called, causing the HP value to rise at a more steady and precise rate.

``````local rate = 10 -- HP/sec
local conn
conn = game:GetService("RunService").Heartbeat:Connect(function(dt)
HP = HP + rate * dt
if (HP >= 100) then
conn:Disconnect()
end
end)
``````

This example accounts for the difference in time between this physics frame and the last physics frame (by multiplying the rate by the time difference). Since it also uses an event rather than a loop, it doesn’t yield the script either.

3 Likes

Can’t wait be effected by client side lag? What I mean is a server side wait(1) would make it wait 1. But can client side lag make the wait(1) <=

Don’t use wait() or wait(x). Use Humanoid.HealthChanged:Wait().

Events are always preferrable over loops simply because they usually resemble a subscriber-listener pattern. This means that there’s no code actively waiting (performing comparison over and over) for a change in the health, but instead a simple link is created so that the waiting (or connected) function fires whenever the underlying event does.
Rule of thumb: The less Lua statements are executed (not written!) to achieve a task, the more performant it will be.

To answer your other question; Given a loop which does nothing else but wait() vs wait(x), the latter is going to (neglectably) perform better. That is simply because function calls on their own are processes with many underlying operations.

2 Likes

Hey there,

Thank you for your detailed answer, however I’m still not sure what to do. While I agree with you that Heartbeat would make a much more fluent while loop, I’m still not sure if it’d be the optimal option performance wise.

Imagine an RPG game where some NPC dies since its HP falls down to 0. The AI script on the NPC starts doing wait() until either the game finishes and the NPC gets deleted, or they get healed.
My question revolves around what yield method to use for the time the NPC will be waiting until it gets raised or deleted. I feel that if I were to use Heartbeat or wait(), the performance would worsen since it’d be checking almost every cycle if HP > 0.
Gameplay wise, replacing wait() with wait(1) would change almost nothing visually, so I was actually considering if that option would be better (again all of this performance wise)

tl;dr: Never use `wait()`. Rarely use `wait(n)` – almost always there’s going to be event you can `Connect` to or `Wait` on instead.