Whats the difference between these 2 type of loops

Heartbeat fires 30 times a second constantly on both server and client. RenderStepped fires every time a frame is rendered on the client. The max FPS is 60, so the RenderStepped fires ~60 times a second unless the player has poor performance. If they get 15 FPS, it fires 15 times a second.

Edit: also, if the player uses a FPS unlocker, it can fire MUCH faster. I’ve unlocked and fired 600 times/sec before on a medium-end PC at the time.

Not really, it’s not tied to the FPS like RenderStepped. Low FPS won’t always drop Heartbeat’s rate like RenderStepped will.

1 Like

then using heartbeat would be alot more unoptimize than while loop since its not linked to the player’s fps

yes thats true but its easier to think of it like everyother frame because roblox caps and trys to stay at 60 fps so thats how games are designed lower performance is unintended, if that makes sense

That depends on how fast you want to fire the loop. Heartbeat fires every 0.033 seconds. If you’re doing complex stuff faster than that, you are begging for poor performance.

Yeah, I’m just saying you need to take that into consideration when deciding which to use.

If a player plays with 15 FPS average, you must expect that it fires half the time that Heartbeat would in that circumstance.

Edit: Correction below

As Heartbeat fires every frame, it runs on a variable frequency . This means the rate will vary depending on the performance of the machine. If the game is running at 40 FPS, then Heartbeat will fire 40 times per second and the step argument will be roughly 1/40th of a second.

i read this on roblox dev wiki it’s still connected to the players machine / fps

1 Like

for loops have a variable so you can see how many times they have repeated

for i=1, 100, 1 do
   print(i) -- 1 2 3 4 5 etc
end

while loops just continue until the condition returns false

local enabled = true
while enabled do
   -- do stuff
end

to be honest you could create a for loop from a while loop, it’s just more convenient to use the for loop

local i = 1
while i <= 100 do
   print(i)
   i += 1
end

To clear up a few things about RunService:

  • RenderStepped fires before each frame is rendered.

  • Stepped fires after rendering, before the next physics step.

  • Heartbeat fires after the physics step, so you can safely use the rest of the frame time without delaying rendering or physics by using this.

They are all tied to framerate as they occur sequentially. You can observe the behaviour on the microprofiler.

They each have an argument that gives the length of time since the last frame, allowing you to account for the variable frequency if using it for something time-dependent like moving a vehicle at constant speed.

To check out the sequential order, as well as other tasks scheduled each frame that you should be aware of, visit Task Scheduler

Cc: @Xeptix @S0MBRX

2 Likes

one eats up a lot of memory while if you were to just yield the infinite loop by a bit it wouldnt.