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.
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)
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.
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.
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.
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.