It’s all in the title, I get that all stuff like renderstepped running before a frame is rendered, stepped before physics is simulated and heartbeat after all that but I’m looking for a list of practical tangible examples where runservice is used like for one a stamina bar that updates on your screen as your character runs.
It is useful in many places! For instance let us assume your having a range around a part, so when the player gets close to the range a GUI pops up, the best method to use in this situation would be using a renderstepped event which calculates the magnitude of the distance every frame. So if the player was closer than t he range we open the gui, else we close it.
@okeanskiy has already got you covered, with the GUI and the use of heartbeat to monitor stamina.
But for other usages of runservices you also got a basic rotating part at constant speed example, there are multiple ways of doing this like using a while true do loop with the delta time from wait() but a runservice connection is more preferable because:
-
It doesn’t yield the current script since it’s in a connection so you can have multiple parts with the same connection in one script. Otherwise you’d have to use spawn() or coroutines which is an additional step that we can avoid using runservice connection.
-
The delta time is already provided in the event as step or dt.
local door = game.Workspace.Door
game:GetService("RunService").Heartbeat:connect(function(dt)
door.CFrame = door.CFrame * CFrame.Angles(0, math.rad(1)*dt*60, 0)
end)
Dude I got the gui stamina thing from okeanskiy, i even mentioned it hoping no one would link me somethng like this -_-, I’m looking for other examples besides that…
Well that’s another, but isn’t that already achieavable with tweening? Why the need for runservice?
Ahh okay, that’s one. What about uses for stepped, what would you need to do before the simulation of physics.
Melee attacks. Since .Touch is terrible for high speed stuff, you can run a raycast every frame using runservice to detect the players.
Melee attacks would go best with heartbeat I presume?
I believe so, but I don’t have much experience with melee attacks.
It’s absolutely necessary when working with a camera, for example if you want to change the default camera behavior. Another example would be if you want an item to be exactly at the center of the camera (like a pet when you open an egg), you could bind the function to render stepped and give it a priority value just above the camera to make sure that it gets rendered after the camera has done rendering. Also, as someone else mentioned, to make sure that nobody gets an advantage because of their high FPS, you should use deltatime (which is basically the amount of time passed since the last frame)