Hello
I looked everywhere for hours and I still don't understand a thing about RunService. I kept watching videos and the dev hub and I still can't understand. If you can help me, that'll be nice.
Hello
I looked everywhere for hours and I still don't understand a thing about RunService. I kept watching videos and the dev hub and I still can't understand. If you can help me, that'll be nice.
Here is all information you need:
https://developer.roblox.com/en-us/api-reference/class/RunService
It has three super important events, the RenderStepped, Stepped and Heartbeat, each of them with for different usages.
These events fire as an alternative to wait() and they fire when a new frame generates or something.
There's also RunService.IsStudio and that is used to determine wether the game is being played in the studio testing environment. Returns a boolean.
RunService:IsClient or RunService:IsGame does the same thing, but returns wether it's a client or game. Returns a boolean.
If you plan on using or playing around with RunService, make sure to take a look at: https://developer.roblox.com/en-us/articles/task-scheduler as well!
If I helped mark as a solution please!
Ok that cleared things. But why would I want to check if the game is being played in studio or something like that. And also do you have any other use cases of run services other than fps thingy and wait?
as others have said, the main use of RunService is to be wait() in loops but better. in fact, it's better than wait at basically everything except simplicity, since wait is often seen as inconsistent to use (you can look up many threads on why not to use wait).
roblox runs in frames. this is just the rate of computations and updates in the game world, every frame the world changes for the user. as you know, better computers have higher fps so they will fire at different speeds depending on the computer, most have like 60.
there are three different types, I'll try to explain each. here's a handy chart.
RenderStepped
it runs at the beginning of every frame before anything else happens. mostly intended for visuals, camerawork or controls because it will update right after the user presses something. it's not a good idea to use it for anything else because it will stop the physics from rendering until it finishes.
Stepped
as you can see on the chart, this is right after everything is replicated and just before physics. the main use of this is to create your own physics in the game if you have any, so your parts move along with the rest.
Heartbeat
this is the one I use the most. it runs after everything else, so it's safer to use, it also doesn't stop anything else in the chain from running. when in doubt just use this one.
BindToRenderStep()
this is a little bit different but worth mentioning, it is quite like RenderStepped but with more control and so I usually use this one more to connect functions to RenderStepped. you can look up more but it has things like RenderPriority.
finally, frames with RunService are not replicated, so the fps and bound functions on Roblox's servers will be separate from the player who might have a slower computer. depends on where you call it.
if you need to do something ALL THE TIME really fast, use RunService. for example, I have enemies that constantly update their behavior using Heartbeat on the server, and used RenderStepped to make the player always face the mouse on the client.
Ok, so first is renderstepped, then stepped, last hearbeat. And if you want to wait, you'd use game:GetService("RunService").HeartBeat:Wait() which will wait after the frame finished rendering and is faster than wait?
And what do you mean by physics? Sorry I am a really bad understanding
yes, you are correct. the physics is just the movement of all of the objects in the game. however, the player characters are separate, they always move before everything else, so if you move the character use RenderStepped.
Ok, so stepped is right before physics renders? And I made a script to wait 5 seconds but it wont work for some reason. Here It Is: game:GetService("RunService").Heartbeat:Wait(5) thx a lot btw
yes. if you used physics in renderstepped, it would be replicated before the other parts and move too early, and if you used it with heartbeat, it wouldn't stall and move way after the other parts.
wait doesn't work the way you think it does, when you use it on something else it doesn't take a specific time, it just waits for it to end. if you want to wait a long period, like five seconds, you can just use wait and it's ok.
if you want a REALLY accurate time though, if you use Connect() on heartbeat or one of the others, as a parameter you will get the amount of time passed since the last occurrence. so you can add that up to accurately do something, maybe 2 times a second. there is an example on the api page.
Oh yeah, I remember someone telling me that if you want to wait a certain amount of time, you'd just do wait(t) and if you are just doing a while loop and you just added wait() no time passed in, you'd just use heartbeat:wait() right?
And let me conclude everything I learn real quick: So first is renderstepped, stepped, last hearbeat. You can use run service to replace while loops and instead of doing wait(), you can do heartbeat:wait(), but if you are just doing wait(t) then that's alright. Also, the parameter "delta time" is just the time between each frame. If you are working with the character or camera, you'd use
renderstepped, but if you are working with physics like you move a part or something, you'd use stepped. But if it's none of that. You'd use heartbeat. RenderStepped happens at the beginning of a frame, stepped is right before physics, and heartbeat is when the frame is finished rendering. Last, you can also check if the script is running on the client, or the server, or if you are in studio!
yeah
This discussion is preserved as it was. New answers, comments, and votes are closed.
3 comments
I said earlier that I don't understand the dev hub and it's too complicated.
They would tell you what it does about each part. RunService is faster than a while true do loop. Only works locally.
Oh, so runservice is just for while loops?