RunService (Heartbeat, Stepped, Script Context) - Roblox Advanced Scripting #24 — Transcript
Full transcript
- 0:00wa what is happening with my Roblox
- 0:02character right now wait oh my goodness
- 0:05look at this effect on my character
- 0:07right now oh my God it is very
- 0:09performance heavy but it it's such a
- 0:11cool looking effect oh my goodness the
- 0:14only way I can see this working is if we
- 0:16used a service that basically allows us
- 0:18to create this sort of frame every
- 0:20single millisecond that's in a loop okay
- 0:23but to tell you the truth I do know what
- 0:24service is causing this to happen and
- 0:26it's called run service that basically
- 0:28allows us to have time management
- 0:30capabilities and also be able to know
- 0:33about script contexts and just to
- 0:34understand more about roblox's Loop by
- 0:37Loop structure if you don't know what
- 0:39any of that means right now don't worry
- 0:40I'm going to go over what all that means
- 0:42so that I can help you understand what
- 0:44is happening here so with that being
- 0:45said I'm going to go a little more in
- 0:47depth to help you understand more about
- 0:48run service before we incorporate it
- 0:50inside of Roblox studio all right let's
- 0:52think about this for a minute I'm going
- 0:53to try and explain to you a little bit
- 0:55of how run service is structured so that
- 0:58we have a better understanding of it and
- 1:00how we can incorporate this inside of
- 1:01Roblox Studio okay so in the past we've
- 1:04worked with managing tasks in how they
- 1:07execute and when they execute within a
- 1:09Roblox game we've done we've done things
- 1:12like task functions with task. weight
- 1:15task. delay that basically determines
- 1:17when tasks are being executed within a
- 1:19script we've also done the same thing
- 1:21with coroutines that basically act
- 1:23similar uh in that regard run service
- 1:26basically gives us a better
- 1:27understanding of this hierarchy that
- 1:29happen happens within a task scheduler
- 1:32and it's important to know uh what tasks
- 1:35are executed before other ones so that
- 1:38we can incorporate our own custom tasks
- 1:41um to execute in this hierarchy as well
- 1:45and that is essentially what run service
- 1:47allows us to do so I want to now show
- 1:50you this concept of roblox's frame by
- 1:53frame Loop structure as we can see right
- 1:55over here so we have a frame happening
- 1:57over here and then we have uh one that's
- 2:00happening at the end here that's
- 2:01basically just going to loop back to the
- 2:03beginning and it's just going to
- 2:04continue executing what does all this
- 2:07mean well first we have to ask what is a
- 2:10frame a frame is basically a unit of
- 2:14game logic that tells us how much work
- 2:16is being done within a specific frame
- 2:19now I'm not going to lie to you a
- 2:21frame a frame executes so quickly that
- 2:25you won't even realize that a frame has
- 2:28been executed because this is where the
- 2:30concept frames per second come in like
- 2:32you know FPS so when we run things that
- 2:35are 30 frames per second or 60 frames
- 2:38per second the more frames that are
- 2:39executing per second the faster quote
- 2:42unquote work is being done because we
- 2:44have more frames to work with when it
- 2:47comes to um how much work is being done
- 2:49within a specific time frame of usually
- 2:52per second basis so when we run tasks
- 2:56tasks are being ran within each frame
- 2:59and it goes from one frame to the next
- 3:01frame to the next frame and then to the
- 3:02next frame and it's just going to keep
- 3:04on going and it's going to be recorded
- 3:06every single second that's going to be
- 3:07happening um within each frame a frame
- 3:11literally passes by every single like
- 3:13half a millisecond so you you won't even
- 3:15realize it happening and so with this
- 3:17loop by- loop structure this is how we
- 3:19are able to record how much uh how much
- 3:22work is being done per time basis so now
- 3:25that we have that understanding we need
- 3:27to understand how tasks are organ
- 3:29organized and executed based on their
- 3:31priority levels so let's say with our
- 3:34task Schuler so our task Scher has all
- 3:38these different types of um I guess
- 3:41priorities when it comes to what task is
- 3:43being executed first and which ones are
- 3:45executed last so the ones that are
- 3:48executed in purple these are events that
- 3:51we can actually use uh with our own
- 3:54tasks with this hierarchy that's
- 3:56happening here so uh the one at the very
- 3:59top this is basically what's being ex
- 4:01executed first so we have the user input
- 4:04and then we go down to this list so with
- 4:06things like rendering weight States
- 4:08garbage collection physics simulation
- 4:10and then heartbeat so obviously we know
- 4:12what user input is it's basically how we
- 4:14detect uh input coming from the player
- 4:17so if I'm moving my character it's
- 4:19detecting the keys on my player and then
- 4:22it goes onto this rendering stage that
- 4:25basically shows that you know this is
- 4:28tasks that these are tasks T that are
- 4:30being run before the frame is even shown
- 4:32into the 3D World and then we have other
- 4:34things like weight States so these are
- 4:36the task functions that we've worked
- 4:37with in the past and then we have things
- 4:39like physics simulation so basically uh
- 4:42any work that's being done in the 3D
- 4:44workspace with the with physics and all
- 4:46that um and then heartbeat which is its
- 4:48own separate thing that we can actually
- 4:50work with in this hierarchy so these are
- 4:53things I want you understand so with
- 4:55this hierarchy this is all within the
- 4:58task scheduler hierarchy and this task
- 5:01scheduler is basically thrown in into
- 5:04each and every single frame uh that's
- 5:07being recorded So with this
- 5:09understanding of the frame by frame Loop
- 5:12and also this task scheduler um
- 5:15hierarchy we're able to get a really
- 5:17good understanding of how tasks are
- 5:20being executed within Roblox and how we
- 5:22can incorporate our own custom tasks uh
- 5:25within this hierarchy and that is
- 5:27essentially how this structure works
- 5:30with run service I hope this makes sense
- 5:33to you because with this understanding
- 5:35now we're going to be incorporating our
- 5:37own customized tasks in this hierarchy
- 5:40with this frame by frame Loop structure
- 5:41so with that being said let's go ahead
- 5:43and go straight into implementing our
- 5:45own tasks within this hierarchy that we
- 5:48have going on over here now that we
- 5:50understand it let's incorporate run
- 5:52service inside of Roblox studio so I
- 5:54hope my analogy made sense to you when
- 5:56it comes to the frame by frame Loop
- 5:58structure and also the way that the task
- 6:00schedule or hierarchy goes when it comes
- 6:02to what tasks are done first versus
- 6:04which ones are done last so the way this
- 6:06is going to work is we're going to
- 6:08create our own custom tasks and execute
- 6:11them using these events that are later
- 6:14that are colored in purple so we're
- 6:16going to start at the very bottom which
- 6:18is going to be the slowest but also the
- 6:20most commonly used one called heartbeat
- 6:23then we're going to step up to uh
- 6:25stepped and then finally render stepped
- 6:26all the way up here so what so the thing
- 6:29that's in common with all three of these
- 6:31events is that they execute tasks very
- 6:34very quickly and it's based on the
- 6:36user's Performance Based on the frames
- 6:38per second uh loop structure that we had
- 6:40going on up here so heartbeat is the
- 6:43slowest one but it's also the most
- 6:45commonly used one so that's what I'm
- 6:46going to show you um right now so here's
- 6:49what we're going to do we're going to go
- 6:50to model and we're going to insert a
- 6:53part essentially what we're going to do
- 6:56is move this part along the xais
- 7:00to constantly move in the negative
- 7:02Direction over here so that we can see
- 7:04it happen in real time once we actually
- 7:06run the script so we're just going to um
- 7:10select this we're going to rename this
- 7:12to heartbeat part and then we're going
- 7:16to insert a script inside of server
- 7:17script service so we're going to hit the
- 7:19plus sign insert a script like this
- 7:21we're first going to make reference to
- 7:22the heartbeat part we're going to say
- 7:24local heartbeat part equals workspace
- 7:27wait for child heartbe part and um more
- 7:32importantly we have to make sure that we
- 7:34actually have the Run service service
- 7:36declared so we're going to say local run
- 7:39service equals game colon get
- 7:42service open close parentheses run
- 7:45service just like this okay so here's
- 7:48how we run our heartbeat event we're
- 7:51just simply going to say run
- 7:53service.
- 7:55heartbeat colon connect function open
- 7:58and close pares there is one parameter
- 8:01here it's called Delta time I'm going to
- 8:03go over what this is in just a little
- 8:04bit but this is the basic structure of
- 8:07how we have our heartbeat event because
- 8:09this is how we connect an event just
- 8:10like this like we we pretty much know
- 8:12this and so next what we're going to do
- 8:13is say that every single time that this
- 8:15is running we're going to change the
- 8:17position of the heartbeat part so
- 8:19heartbeat part. position equals
- 8:22heartbeat part.
- 8:25position plus Vector 3. new open and
- 8:29close parentheses we're going to put in
- 8:32a -1 comma 0 comma 0 because1 on the
- 8:36xaxis will allow this part to go in this
- 8:39direction like this so if we now go into
- 8:41the game hit test and hit run then what
- 8:44we should see with the part is that it's
- 8:46going to move uh one stud in that
- 8:49direction very very quickly now once
- 8:51again uh heartbeat is running every
- 8:54single frame so like I mentioned here
- 8:56before so every single time a frame is
- 8:58is being made then it's going to move
- 9:00the part negative-1 in the uh xaxis and
- 9:03it's going to do it again and again and
- 9:04again and again and it's going to run
- 9:06very very fast like uh if I were to
- 9:09visualize how fast this is we can do a
- 9:11print here just by saying heart uh beat
- 9:14like this and then we go into the game
- 9:17hit run if we look at the output we can
- 9:19see that it's literally firing like
- 9:21hundreds of times like this is how fast
- 9:24heartbeat runs it's it's actually really
- 9:26insane how fast heartbeat does run and
- 9:29keep in mind this is at the very lowest
- 9:32tier of the task scheduler so if we go
- 9:34up here it's going to run even faster
- 9:36than the one down here which is actually
- 9:38pretty insane so like I mentioned uh the
- 9:40significance of heartbeat is that it's
- 9:42the slowest but it's also the most
- 9:44commonly used one and the reason we use
- 9:46heartbeat out of all these other ones is
- 9:47because this comes after physics
- 9:50simulation so basically like if we have
- 9:52our player that's already running that
- 9:53we already had our user input down for
- 9:56then uh heartbeat is going to be
- 9:58everything that comes after this so
- 10:00that's why we use this one out of all
- 10:02these other ones now here's some tips I
- 10:04have for you when it comes to using this
- 10:06and you know why we should use it so if
- 10:07you were to go back to our script you
- 10:09might think why don't we just use a
- 10:11while task. weight do like this and then
- 10:15just copy everything that's that's in
- 10:17here inside of here we could do this
- 10:19instead of using heartbeat but once
- 10:21again if you imagine where heartbeat is
- 10:24inside of this task scheduler um
- 10:26hierarchy heartbeat is all the way down
- 10:28here but then we can consider what's
- 10:29going to be all the way back here with
- 10:31weight states with task. we task. spawn
- 10:33delay so you might ask like you know why
- 10:36why is there a difference like why is
- 10:37there a significance when it comes to
- 10:39this let's imagine this in terms of how
- 10:42the script is handling what's happening
- 10:44here if we were to use a task. weight
- 10:47for this wild true do statement we are
- 10:49literally pausing execution of the
- 10:51script before we go in and try to ex
- 10:55whatever is going on down here and this
- 10:57is a fixed length keep in mind as well
- 10:59so Tas task. weight with nothing in here
- 11:02could be like
- 11:040.1 this is going to be a fixed number
- 11:06throughout the entirety of the game's
- 11:08duration but if let's say one of the
- 11:10players had bad performance issues with
- 11:13their device where um the frames for
- 11:15them run slower than if we had a in
- 11:19comparison to a gaming PC that can run
- 11:22really fast this can run really fast
- 11:24versus somebody who can run very slow
- 11:27with like their iPhone 4 or something
- 11:29all of these frames really depend on the
- 11:31performance and that's why run service
- 11:33matters so much when it comes to when
- 11:36tasks are being executed because it's
- 11:37primarily based on the users's
- 11:40performance of the device that they're
- 11:42playing on so that's why instead of this
- 11:44fixed length um if we were to let's say
- 11:47be playing on a mobile phone then it
- 11:49would drop down to this versus what it
- 11:51would be like if we were playing on a
- 11:52gaming PC like this it really depends on
- 11:56what device the player is playing on so
- 11:58that is one of the the so that is a
- 12:00really big difference when it comes to
- 12:02uh heartbeat over using using like a
- 12:05task. w while loop like this so here's
- 12:07another tip I have for you I wouldn't
- 12:09encourage you to use heartbeat or any of
- 12:11these other run uh events uh throughout
- 12:14the entire duration of the game I think
- 12:16it's better for you to wrap this in a
- 12:17temporary connection and then disconnect
- 12:20it whenever you don't need to have it
- 12:22anymore so here's um so here's an
- 12:24example if I were to wrap this uh
- 12:27connection inside of a local variable by
- 12:29saying uh local connection equals run
- 12:32service heartbe connect function so so
- 12:34as soon as we run the script we're going
- 12:36to have a variable be set equal to this
- 12:39connection right here and it's
- 12:40automatically going to fire so then if
- 12:42we were to let's say have a task. weight
- 12:44for about five seconds over here then it
- 12:46we're literally going to disconnect this
- 12:49connection so that we can stop heartbeat
- 12:51from continuing after we don't need it
- 12:53for a while so we're just simply going
- 12:55to say connection colon disconnect like
- 12:59this like it literally disconnects the
- 13:01connection that we've created over here
- 13:04by placing this inside of a variable so
- 13:07what happens now is if we hit run and
- 13:09then we see this change happen with the
- 13:11part moving so fast in that direction
- 13:14for about 5 seconds it's literally just
- 13:16going to stop right there because we
- 13:17waited 5 Seconds um the connection for
- 13:20heartbeat literally stopped after we
- 13:22decided to hit the disconnect on that
- 13:25connection so this is very effective if
- 13:27you want to temporarily have uh events
- 13:30be connected and you can disable them if
- 13:33you don't want that connection
- 13:34continuing anymore after that so that's
- 13:36another tip I have for you and I hope
- 13:38you find this to be useful okay but now
- 13:40going back into our heartbeat function
- 13:42there is this parameter called Delta
- 13:43time and you might ask what is this
- 13:46thing being passed into the function
- 13:48Delta time basically tells us how much
- 13:51time has elapsed from the current time
- 13:54versus the time that it took to start
- 13:56the last frame so if we were to go back
- 13:58into our script so basically whenever
- 14:00this Frame fires uh it's going to detect
- 14:03how much time has passed from when this
- 14:05Frame started versus when this Frame
- 14:08started and so this basically gives us a
- 14:10good uh indicator of how much time has
- 14:12passed in between these two frames and
- 14:14this is useful for making precise
- 14:17calculations based on the performance of
- 14:19The Player's uh uh device and a really
- 14:22good example I can think of this is if I
- 14:24had let's say A Part here that rotated
- 14:27based on the player's device performance
- 14:29so if we were to let's say smoothly uh
- 14:32rotate this part like let's just imagine
- 14:33this part was smoothly rotating it's
- 14:35going to either move slowly or fast
- 14:38depending on the player's performance
- 14:40and this is very useful for optimizing
- 14:42the player's experience depending on
- 14:44their devic's performance so that's a
- 14:46really good example I can think of but
- 14:48we can incorporate this inside of our
- 14:50script as well and I'm going to show you
- 14:51how we can do this so instead of having
- 14:55a fixed Vector 3 value with um negative
- 14:58-1 each time time the position is being
- 15:00changed what we can do instead is create
- 15:03a local variable called distance and we
- 15:05can set this equal to Delta time and
- 15:08then we can have like a multiplier here
- 15:10just by I don't know saying like 10 in
- 15:12this case and then we can replace the
- 15:14ne1 here which is a fixed number to
- 15:17distance and I'm also going to make this
- 15:20multiply by -10 so that it goes in the
- 15:22negative Direction okay so now that we
- 15:24have this let's go into the game and hit
- 15:27run and see what happens so now now that
- 15:29we have this the part is moving slower
- 15:32than it should be oh well it's actually
- 15:33like in the way of these frames my bad
- 15:36so let me actually just move all of
- 15:38these really quick and then hit run
- 15:40again so now what we should see is this
- 15:43part is constantly going to be moving uh
- 15:45in this direction until it just stops
- 15:47after like 5 seconds because it's now
- 15:50dependent on the player's performance if
- 15:52the player was uh having a slower device
- 15:54then this part would have moved even
- 15:56slower than it should have versus um a
- 15:59faster versus a faster device than this
- 16:02part would be moving faster because of
- 16:03it and um if we want to just quickly
- 16:05measure like what the Delta time
- 16:07actually is um we can just print this
- 16:09out in the output so that we can see uh
- 16:11how much time is being elapsed uh before
- 16:14the other frame so as you can see it's a
- 16:16very small number if um if we decide to
- 16:19use heartbeat over anything else so
- 16:22those are a few optimization tips that I
- 16:24have for heartbeat and also the
- 16:27explanation of using Delta time I hope
- 16:29this part has made sense to you so far
- 16:31up until this point okay so the next
- 16:33event is stepped and this comes in
- 16:35before the physics simulation and so the
- 16:39main difference between stepped and
- 16:41heartbeat is that stepped is executed
- 16:43before all of the physics in the game is
- 16:45being executed while heartbeat is
- 16:48executed after all the physics
- 16:50simulations have been made so this tells
- 16:52us that if we want to use stepped this
- 16:55would primarily be used if we want to do
- 16:57something related to phys physics before
- 17:00executing the actual physics simulation
- 17:02of our 3D World so in this case if we
- 17:04wanted to make changes to like let's say
- 17:06A C frame of a part or if we wanted to
- 17:08move the velocity or just like other
- 17:10things like that we would use stepped
- 17:12over heartbeat and another added bonus
- 17:14to steep is that this executes before
- 17:16heartbeat so this executes more
- 17:19frequently than heartbeat at a faster
- 17:21rate so this one works even faster than
- 17:24heartbeat in a basic sense so let's do a
- 17:28side by-side comparison with another
- 17:30part in relation to heartbeat part so
- 17:32I'm going to duplicate this heartbeat
- 17:35part and move it over here I'm also
- 17:36going to uh change the colors of these
- 17:39so that we can differentiate between the
- 17:40two um just like this and I'm going to
- 17:43call this uh stepped part so now what
- 17:48we're going to do is go back into our
- 17:49script we're going to just simply copy
- 17:52this code right here and then paste it
- 17:54down here we're going to call this
- 17:56connection connection 2 and then we're
- 17:58going to disconnect connection 2 after 5
- 18:01Seconds just like we did with the first
- 18:03connection just like this and then
- 18:06instead of heartbeat we're going to
- 18:08replace this with Ste now what's
- 18:10interesting about Ste is that this
- 18:13function gives two arguments so not only
- 18:15does it give the Delta time but but it
- 18:17also gives the time I'm just going to
- 18:19call this duration time because time is
- 18:20a reserved keyword based on roblox's
- 18:22standards so this time basically tells
- 18:25us that uh this is how much time has
- 18:28elapsed ever since the game started but
- 18:30this is how much time has elapsed ever
- 18:32since the last frame was executed so
- 18:35there is a difference between the two
- 18:37and you need to make sure that you do
- 18:38know when to use either one of these
- 18:40things now generally you don't really
- 18:42need time but I would still say Delta
- 18:44time is pretty important uh I'm also
- 18:46going to make reference to the stepped
- 18:49part so I'm going to copy this paste it
- 18:50like this and then say stepped part wait
- 18:54for child stepped part I'm going to copy
- 18:57this and I'm going to find every
- 18:59instance of heartbeat part and replace
- 19:01it with stepped part instead just like
- 19:03this okay so now that we have this taken
- 19:05care of let's go and do a race we're
- 19:07going to do a test comparison of how
- 19:09much time will uh elapse with this
- 19:13happening okay so as you can see
- 19:15heartbeat is running faster but what
- 19:17does this tell us I thought uh stepped
- 19:19was supposed to be faster the reason
- 19:21this is happening is because stepped
- 19:24executes a lot faster than than
- 19:26heartbeat does and whenever we are
- 19:29recording the distance we are using the
- 19:30Delta time from the current frame to the
- 19:33last frame and so the distance is going
- 19:36to generally be smaller than heartbeat
- 19:39but we're going to be using but we're
- 19:42going to be executing this position
- 19:44change more frequently than this one so
- 19:47as a result heartbeat uh executes less
- 19:51but it has a bigger distance but stepped
- 19:54has uh more execution but has a smaller
- 19:57distance each time we executed which is
- 19:59really interesting so in essence when it
- 20:02comes to knowing which one is faster
- 20:04there's a lot of variables that tie into
- 20:06it and there's a lot of definitions when
- 20:08it comes to which one is quote unquote
- 20:10faster so that's why you need to
- 20:12understand this from the perspective of
- 20:14the hierarchy of like do you need to
- 20:17make calculations before the physics is
- 20:19actually running or do you need to make
- 20:21calculations after the physics is
- 20:22running I would say this is what's more
- 20:24important uh when you're trying to
- 20:26decide which one of the two you need to
- 20:27use but if worse comes to worse if you
- 20:30don't know which one to use just use
- 20:32heartbeat like there's nothing wrong
- 20:33with using heartbeat uh this is going to
- 20:35be basically for more uh specific cases
- 20:39if you do need to use them so that is
- 20:41hopefully an analogy that helped you
- 20:43understand the difference between
- 20:44stepped and heartbeat if you decide to
- 20:46use either one of these in the future so
- 20:48we finally reached the last event on
- 20:50this hierarchy called render stepped and
- 20:52as you can see this is all the way up
- 20:54here in the hierarchy as being part of
- 20:57the rendering stage of this task
- 20:59scheduling um list so when we use render
- 21:03you can run stuff very fast with this
- 21:05thing like at a very very frequent rate
- 21:07but there's a few things that we need to
- 21:09keep in mind about render so the first
- 21:12thing that's really interesting is that
- 21:13we can only use render Ste on the local
- 21:16side we can't use this on the server
- 21:18side like we've been doing with
- 21:20heartbeat and Ste this can only be done
- 21:22by the local player so that is one very
- 21:24important distinction to know about when
- 21:26it comes to using render step is we can
- 21:28only use it on the local side and so
- 21:30with that being said I'm going to show
- 21:31you a demonstration of using render
- 21:34using gys rather than using parts to
- 21:37move them around using physics and all
- 21:38that sort of stuff so what I'm going to
- 21:40do is go to starter GUI hit the plus
- 21:43sign create a screen GUI I'm also going
- 21:46to create a a text label like this uh so
- 21:49that we can see it on our screen and
- 21:51then I'm just going to align it like
- 21:53right here like so and then I'm going to
- 21:55insert a local script inside of the text
- 21:57label so the first thing I'm going to do
- 21:59is make reference to the text label
- 22:02equals script. parent and I'm also going
- 22:05to take the Run service declaration
- 22:08paste that into here and then I'm also
- 22:10going to take this task. weight
- 22:13statement and also this connection
- 22:14disconnect um statement right here okay
- 22:17so now what we're going to do is right
- 22:19before the task. weight statement we're
- 22:20going to make a variable called local
- 22:24connection equals run
- 22:26service do render stepped colon connect
- 22:32function open and close parenthesis
- 22:34enter now this one only takes in one
- 22:37argument which is going to be the Delta
- 22:39time which is very similar to to our
- 22:42heartbeat connection rather than our Ste
- 22:45connection which has two parameters in
- 22:46there so now I'm just going to Simply
- 22:48calculate a um distance I guess we could
- 22:51just say local distance equals Delta
- 22:54time time 100 like this and so what do
- 22:58we want to do with this distance well
- 23:00what we can do we can basically rotate
- 23:03this part clockwise based on the
- 23:04player's performance so that we know
- 23:06that render step is working as expected
- 23:08so first thing we can do is probably
- 23:10just make a print statement here by
- 23:12saying render St like this so that we
- 23:14actually know how many times this is
- 23:16being run and then what we can do is say
- 23:19text label. rotation equals text label.
- 23:24rotation plus distance like this all
- 23:28right so in order to test this we're
- 23:30going to have to go into the game hit
- 23:32test and then we have to hit play so
- 23:34that our actual character is going to be
- 23:36inside of the game all right so now that
- 23:38we have this we can see that it's
- 23:40constantly rotating uh from where it
- 23:42started uh to constantly just go
- 23:45clockwise like this until about 10
- 23:47seconds where we just disconnect the
- 23:48connection just like that but as you can
- 23:50see within this uh 10-second time frame
- 23:52uh it ran um very very frequently like I
- 23:56can't even describe how frequently
- 23:58render Ste executed but it it executed
- 24:01very very frequently at at least I can
- 24:03tell you that faster than what Ste could
- 24:05execute faster than what heartbeat could
- 24:07execute but once again this can only be
- 24:09executed on the local side so there are
- 24:12trade-offs to using render step but I
- 24:14recommend trying to experiment and
- 24:16figure out which ones work best for you
- 24:18when it comes to when it comes to
- 24:20knowing which event to use because
- 24:22render it's all the way up here in the
- 24:24rendering process so you can decide for
- 24:26yourself whether this is the right step
- 24:28for you to use in your scripts or if you
- 24:31just want to use stepped or heartbeat
- 24:32like it honestly doesn't really matter
- 24:34too much of a difference which one you
- 24:35use but it's still good to understand
- 24:38why there is a difference between these
- 24:40three and how this task scheduling
- 24:41hierarchy works so I hope this part has
- 24:44made sense to you up until this point so
- 24:46we've pretty much gotten over the basics
- 24:48of time management when it comes to run
- 24:50service and what it has to offer for
- 24:53executing tasks at specific times during
- 24:56specific occasions that we'd want uh to
- 24:58execute them but run service has
- 25:00something else that we uh May deem to be
- 25:03useful and that is knowing the context
- 25:06of scripts being executed and what I'm
- 25:08trying to get out here is run service
- 25:10has properties to tell us if let's say
- 25:12we're playing the game in studio or if
- 25:14this script is being run on the server
- 25:16or if the script is being run on the
- 25:17client we can tell based on the context
- 25:20of where the script is being executed
- 25:21and if that's the case then we can
- 25:23perform specific tasks based on specific
- 25:27criteria like that uh an example I can
- 25:30think of is if let's say I was
- 25:32programming a boss battle uh and it has
- 25:34cutscenes and everything like that then
- 25:36i' probably just want to skip the
- 25:38cutscenes if I'm playing inside of
- 25:40Roblox studio just so that I can test
- 25:42the boss um and that's just one example
- 25:45I can there's so many examples you can
- 25:46probably think of if you want to do
- 25:48something specific with your scripts if
- 25:50you're playing in Roblox Studio versus
- 25:52if you're playing in the actual game and
- 25:54we can do the same thing with knowing if
- 25:56a piece of code in a script is being
- 25:58executed on the server versus if it's
- 26:00being executed on the client and this is
- 26:03also very useful for module scripts
- 26:05which I'm going to show you in just a
- 26:06little bit so for the rest of this video
- 26:08I'm going to show you these properties
- 26:10that we can use inside of Roblox studio
- 26:12so let's go ahead and go straight into
- 26:13that all right so let's start with
- 26:14something easy which is basically
- 26:16determining if we are playing the game
- 26:18inside of Roblox Studio or not so here's
- 26:20how it's going to work I'm going to go
- 26:22on the right side and just very quickly
- 26:24create a new script I'm going to call
- 26:26this script uh context like this and
- 26:29then what we're going to do is first
- 26:31make declaration to run service I can
- 26:32just go to one of my previous scripts
- 26:34and duplicate that place it into here
- 26:36and then here's all we need to say we
- 26:38can just say if run service colon is
- 26:43Studio open and close parentheses then
- 26:46print player is playing in studio and
- 26:51then if we don't do that then we can say
- 26:54player is not playing in studio
- 26:58so it seems very easy right we just go
- 27:00into the game hit test hit play and then
- 27:02on the output what we should see okay
- 27:04well if we uh if we just disabled our
- 27:07previous script for for for a minute
- 27:10let's go back into the game and then we
- 27:11should see that player is playing inside
- 27:14of Studio because we are indeed playing
- 27:16inside of studio if you were playing in
- 27:18an actual game then it's just going to
- 27:20go to the L statement and then go down
- 27:21here by saying player is not playing in
- 27:23studio and not only do we have checks
- 27:25for if the player's in studio we can
- 27:27also check if the player is I don't know
- 27:29play testing or if they're in run mode
- 27:32uh all of these different things we we
- 27:33can check for all of these different
- 27:35contexts if we were to have them inside
- 27:37of a script uh to perform different
- 27:39things based on the script context so
- 27:41that's one batch of these functions that
- 27:43I'm going to show you here and it seems
- 27:44to be very self-explanatory it's pretty
- 27:46easy okay so now I'm going to show you
- 27:47some other ones like is server and is
- 27:51client now if you've been scripting for
- 27:53a very long time and you've been
- 27:54following the series I think you know
- 27:55the difference between the server and
- 27:57client is I don't need to explain this
- 28:00um but we can basically check in a
- 28:01single script if we are playing on the
- 28:04server or if we are playing on the
- 28:05client based on the script's execution
- 28:08and uh which context we are executing
- 28:11this script from so kind of like what we
- 28:13had before up here we could just say if
- 28:15run Service uh colon is server then we
- 28:20can print that we are on the server uh
- 28:22if we say if run service is client then
- 28:27we can print or just like do whatever is
- 28:29going to be here if we are playing on
- 28:31the client so both of these are being
- 28:33checked inside of the context of this
- 28:36script inside of serers script service
- 28:38you kind of have to think outside the
- 28:39box a little bit to really understand
- 28:41and think about when this would be
- 28:44useful like when would you want to check
- 28:47if this uh execution is happening on the
- 28:49server or if this is happening on the
- 28:51client I'm going to show you one example
- 28:53of using this that I find to be pretty
- 28:55helpful so I'm just going to delete this
- 28:56code that we have over here and what I'm
- 28:58going to do is create a module script on
- 29:00the right side of replicated storage so
- 29:02I'm going to hit the plus sign I'm going
- 29:03to create a module script right over
- 29:05here and basically the point of this
- 29:07module script is I'm going to create a
- 29:09function and this is going to check if
- 29:12uh wherever this function was executed
- 29:14from whether that was from the server or
- 29:16the client are we going to continue with
- 29:18the execution of this function so I'm
- 29:20going to show you what this looks like
- 29:21I'm going to say function module do uh
- 29:24we could just say like my function or
- 29:26something like that open close print es
- 29:28enter so what we're going to do is get
- 29:32reference to our run service I'm just
- 29:33going to copy this paste this up here
- 29:35I'm first going to say if run
- 29:39Service uh colon is client open close
- 29:42parentheses then we're just simply going
- 29:44to return this and then uh we're going
- 29:47to make a print statement down here by
- 29:49saying um my function has been executed
- 29:54and you can just like have whatever
- 29:56whatever else is happening down here so
- 29:57let's try understand what's happening
- 29:59here when we run my function it's first
- 30:01going to check if whatever called this
- 30:04function is on the client then we're
- 30:05just going to return it because let's
- 30:07say a hacker is uh trying to execute
- 30:10that they see inside of this module
- 30:11script well since they're on the client
- 30:13if they try to activate this function
- 30:16and it sees that it's being activated
- 30:19from the client then we're just not
- 30:20going to do anything about it we're just
- 30:21simply going to return it but if we are
- 30:24executing this on the server then we're
- 30:26going to drop down here and then
- 30:27continue off with whatever we were
- 30:29trying to do with our function just
- 30:30execute it like normal so now I'm going
- 30:32to show you what this looks like if we
- 30:34were to uh require this uh module script
- 30:37and try to execute it so we're going to
- 30:38drop two lines down here and say local
- 30:41module equals
- 30:43require game. replicated
- 30:47storage. module script like this and
- 30:50then all we're going to do is just say
- 30:52module. my function open and close
- 30:54parentheses just like that so we are
- 30:56running this on the server so if we go
- 30:58into the game hit test and hit play then
- 31:00what we should see in the output is that
- 31:02my function has been executed because we
- 31:05are playing on the perspective of well
- 31:07okay we're playing in the perspective of
- 31:09the the player but we can see down here
- 31:11highlighted in green that means this was
- 31:13printed on the server so my function has
- 31:15been executed by the server as expected
- 31:18so now let's see what happens if we
- 31:20decide to change is client to is server
- 31:23instead so that means this can only be
- 31:26executed on the client
- 31:28so if we go into the output we can see
- 31:30that player is playing in studio but we
- 31:33do not see the print statement for uh my
- 31:36function has been executed because we
- 31:38executed this function on the server so
- 31:42because we are now uh seeing if we're
- 31:44trying to be on the client it's just not
- 31:46going to run and um if we try to do this
- 31:49the other way around too so I'm
- 31:50basically just going to very quickly
- 31:52create a local script inside of starter
- 31:54player starter player scripts I'm just
- 31:56going to create a local script
- 31:58I'm going to copy this run service
- 32:00command here and these two down here
- 32:04what I'm going to do is just quickly
- 32:07disable my context script inside of
- 32:09server script service go into the game
- 32:10hit test and hit play so we should see
- 32:12in the output is absolutely nothing
- 32:15because we are trying to see if we are
- 32:18executing this on the server but if we
- 32:21do this the other way around with is
- 32:23server then what we should see in the
- 32:25output is finally my function has
- 32:28executed because we are calling this on
- 32:29the client and we are seeing in the
- 32:31module script if this is a client or if
- 32:34this is not a server I mean if this is
- 32:36uh a client then we're going to execute
- 32:38what's going to be down here you get the
- 32:40idea at this point um you kind of have
- 32:42to think a little bit of how you can
- 32:43incorporate is server or all of these
- 32:45other context uh functions inside of
- 32:48your script and so that is another
- 32:51really cool and neat thing you can do
- 32:53with run service that other services do
- 32:55not really provide so
- 32:58I hope you found this part to be uh
- 33:00informative as well and it can benefit
- 33:02you for your Roblox Development Career
- 33:05journey I hope you found this episode to
- 33:07be informative this is one of the more
- 33:09complicated Concepts in Roblox Ser
- 33:11development so if you've made it this
- 33:12far into the series then I could then I
- 33:15got to congratulate you on that cuz
- 33:17these Concepts they're still going to
- 33:19continuously become more and more
- 33:21difficult as time goes on within the
- 33:22series and you should be proud of it so
- 33:24to recap what just happened we learned
- 33:26about the frame by frame Loop structure
- 33:28that Roblox has we learned about the
- 33:30task scheduler hierarchy that happens
- 33:32here we learned about uh how we can
- 33:35incorporate our own uh how we can
- 33:37incorporate our own tasks into uh the
- 33:40task scheduler and also how we can run
- 33:43certain contexts uh inside of our
- 33:45scripts to determine um what's being
- 33:48executed and how we can execute the
- 33:50script based on the context of of where
- 33:52it was executed from so with everything
- 33:55that I've taught you in this video I
- 33:56hope you put it to Great use inside of
- 33:57your Roblox scripting and with that
- 33:59being said that is pretty much going to
- 34:01be it for this episode if you want to
- 34:02learn more about time management and
- 34:05also executing tasks during specific
- 34:07times within the game then I encourage
- 34:08you to watch my core routines video
- 34:10which I will have a link for you right
- 34:12here in this video but with that being
- 34:13said I will see you in the next episode
- 34:17take care
About this transcript
This page contains the full transcript of RunService (Heartbeat, Stepped, Script Context) - Roblox Advanced Scripting #24 by BrawlDev, generated from the public captions YouTube serves with the video. The transcript has 6,679 words across 903 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.