Why use RunService Heartbeat vs while loop?

I’m modifying an NPC, and they use RunService Heartbeat to decide logic for the AI like run to target or attack. And it doesn’t make sense to me. From what I’ve read this function runs at x revelations per frame.

So 60 fps = 60 run times per second. Why would anyone want this for an NPC? Why not use a while loop and loop every 0.1 seconds? Wouldn’t that be more efficient?

False.

Using wait() is inefficient in big games or codes that rely on performance.

1 Like

Also, 60FPS has no visual difference with waiting every 0.1 seconds.

Instead of wait(), use task.wait() instead.

The reason heartbeat is used is to maintain a balance between the AI’s responsiveness and performance as it needs to respond to the games physics engine changes (characters it’s chasing moving around)

Let’s consider this scenario:

Imagine per chance during the 0.1 seconds wait let’s say in at the 0.02 second mark the character the NPC is chasing teleports. The code will then have to wait a remaining 0.08 seconds or

like 0.08 seconds/ (1/60 frame per second) = around 5 frames which is needed for the NPC to respond to change.

However if you use heartbeat it is guaranteed to run when these physics changes happen hence allow the NPC to respond to the changes caused by the physics engine better and smoother, instead of yielding for a couple of frames.

This is from a theoretical standpoint. Practically as you notice the 0.08 seconds of wait is not really noticeable you might get away with it. However perhaps with heartbeat the code will respond with 0 wait only dependent on well you guess it the variable frame rate of 60 fps.

It’s a trade off.

Measure the activity rate, notice in game how it goes, see what works best.

But in this case I’m programming a zombie. It doesn’t need excellent tracking. So it’s ok if he’s off in tracking by even 2 seconds really.

If I did a while loop with a 0.5 wait. Would that not be more efficient than running every frame?

And what is task.wait? And what’s the difference between wait and that?

wait() will be deprecated soon since 0.03 seconds is an arbitrary amount of time and it can cause unintended behavior. task.wait() waits until the next frame and it is equal to RunService.Heartbeat:Wait()

Gotcha.

How would you sync 2 players together, like an event that happens simultaneously for 2 users. I guess you use tick()

But isn’t there always going to be a delay using RunService if 1 player is lagging really bad with choppy fps?

Like lets say I’ve got a script that everytime it runs it gives x player cash every time it runs. If I’m using HeartBeat. And running it off that, isn’t the player with the higher fps going to have more cash added due to the script running faster on his PC?

If you use Heartbeat on a LocalScript then you would need to account for differences in FPS with the dt (delta time) parameter. If you use Heartbeat on a Script for the cash giver, then, from what I’ve tested, it would run at a constant rate on the server for everybody.

EDIT: Just tested it - the server fires Heartbeat 60 times per second, regardless of the client’s FPS.