I was just wondering when should I use each one and with examples?
Each of these run once a frame, but they run at different times during the frame. Here’s a helpful diagram to show this, made by zeuxcg and prettified by Fractality_alt:
Examples for when you’d use each:
RenderStepped runs before anything renders, so stuff like camera manipulation that’s used to display immediate feedback for the player should be put here. It also blocks rendering until the connected function is done, so don’t put anything too slow in here.
Stepped runs before the physics step, so any custom physics that take precedence over Roblox physics should be handled here. Since it blocks physics, you also don’t really want to put anything too slow here.
Heartbeat runs at the end of the frame. You should use it for anything that needs to run once a frame, but is a slow enough operation that you don’t want it blocking anything. It’s generally used as a go-to preferable replacement for anything that was using a
while wait() doloop. If you aren’t sure which event to use, it’s better to stay safe and go with Heartbeat since there’s no risk of blocking Roblox processes.
Heartbeat:Wait()can also be used as a faster replacement for
wait(), since it returns every frame while
waithas a minimum of a thirtieth of a second (about two frames).
Never completely understood that diagram. How come frame 2 have different things under render than frame 1?
Like I said,
wait has a minimum wait time of a thirtieth of a second. It only resumes every other frame. I’d assume the same is true for network replication.
So if I’m getting this right, when does rendering happen? Before or after Stepped/Heartbeat?
Or does it happen in parallel. If it does happen in parallel, how do I make sure that things under stepped / heartbeat render at that time for after the renderstepped on the next frame?
Well posatta explained that Heartbeat runs at the end of each frame, Stepped runs before any physics happen (It happens before a part falling), RenderStepped is before any render (I assume renders are frames as well but I dont know)
Hello anyone coming in from Google and the great beyond! Looks like this thread pops up frequently when people are searching for which of these events to use. At the time of this question’s writing, the Task Scheduler article wasn’t yet published, but now… it is! Nonetheless, @rogchamp’s original response is pretty solid.
You can make an informed choice by checking out the Task Scheduler article! Some general rules of thumb, mostly echoing the contents of the article and the solution:
- Gameplay-related stuff should use RunService.Stepped. Examples:
- ticking damage-per-second effects
- setting physics properties each frame (remember, Stepped happens before physics update)
- Rendering-related stuff should use RunService.RenderStepped. Examples:
- Re-positioning the camera
- Updating your own tweens (not ones started using TweenService)
- Re-positioning a ring in-world to indicate an area of effect under the mouse
An important distinction: Heartbeat fires even when the game is paused, whereas Stepped will not fire. This doesn’t mean you should always use Heartbeat, though! Make an informed decision based on what your game is doing each frame.
So, for example, if I want a wind factor on a golf ball, stepped would be the right choice?
You never got an answer on this thread, though I figure someone out there’s probably looking for an answer to this. Anyway…
For what it’s worth, a BodyForce is what you’d want to use for this case. Wind exerts translational force on the ball, and would probably be defined using world space…which is how BodyForce works. If the force needed to change based off location (for example), you would use Stepped to update its Force property.
Alternatively, you could modify the golf ball’s velocity property directly, but that can have some unwanted side effects.