Heartbeat is worse than while true do wait()?

Hey! I recently was discussing heartbeat with a discord member in a server I am active in.
They said something that I wasn’t too sure of, and I wanted to make sure that they were right when making their claims, as they seem to make heartbeat worse than wait.


Is this true?

Thanks in advance!

3 Likes

Heartbeat rate is variable, but I don’t think you’re gonna ‘beat’ it with wait(). Heartbeat supplies the ‘dt’ so you can account for the fps achieved at any given time. If there is any contention for resources the hearbeat is gonna happen before wait() which would be a lower priority in the schedule. (I think)

No it isn’t, heartbeat uses client fps if it’s being used on the client.

5 Likes

This is incorrect, heartbeat has its uses for different applications.
As it says in the screenshot, heartbeat runs after rendering, meaning that it should be used if you want to run something every frame which doesn’t involve rendering.

For example, manipulating the camera or player input should be done on renderstepped. However, if you have a lot of code in renderstepped which doesn’t involve rendering, it could lag your game, which is why hearbeat would be used in those cases.

2 Likes

Your friend only described the downsides of Heartbeat.

wait() is a code smell, and it is only useful if you are yielding a thread with smaller numbers, however if you use it to yield a larger number, that is where this function will be broken as it will yield longer. RunService.Heartbeat fires AFTER the physic simulation of the game is completed inside a frame, even though it might not be better when comparing to wait() when yielding a small amount of time, it is extremely useful for higher yielding.

However, task.wait() is basically the best option since it yields the thread for an amount of seconds then resumed it on the next Heartbeat event, but task.wait() still has the same code smell as wait(), just slightly more efficient.

2 Likes

False, Heartbeat only fires when physic simulation in the game is completed inside a frame.

IIRC this is incorrect and task.wait() is actually equivalent to RunService.Heartbeat:Wait() with a few optimizations. wait() is not the same as either and under the hood uses an entirely different method of yielding.

False, here’s a reply about why:

And you should probably read the announcement better as it said:

1 Like

Ah that’s really interesting thanks for correcting me

right here they say that they are equivalent in behavior

EDIT:
saw your second quote
that guy talks about what they return, which isn’t really behavior

1 Like

But still, it does kinda give a bit hint of the behavior according to whatever’s that’s returned.

they still behave similarly either way, I don’t think WallsAreForClimbing was even talking about what they return at all

I am just giving him a suggestion about yielding, although yeha that’s off topic by still worth to bring it up.

Nope, first one is just bad coding if that situation does happen with stability problems in low fps. Heartbeat is able to account for game slowdowns with the use of delta time. However the programmer must be able to take into account of this in their game.

Additionally wait() has it’s own stability problem proven with known big game developers like clone trooper in their test replication, wait() is also frame dependent if you look at the task scheduler article.

Edit: I believe this is because wait() is limited to 1/30 of a second while heartbeat is 1/60, they will never change this because this might cause problems in users code (they might wait() for a service to load at the top of the script). Article about it being 30 Hz or 1/30 of a second.

Second claim heartbeat is based on servers fps is just false, it’s client fps.

That is why you commonly see people having issues with the opposite instead where the client uses fps unlockers and the code doesn’t account for that decrease in delta time.

4 Likes

Thanks you! And everyone else who answered.

I didn’t expect this topic to get as many replies as it did!

I still have to read some of the replies for a better understanding.
Thank you all again!

1 Like