YouTube2Text

RunService (Heartbeat, Stepped, Script Context) - Roblox Advanced Scripting #24 — Transcript

by BrawlDev · 6,679 words · 903 segments · language en · Watch on YouTube

Full transcript

  1. 0:00wa what is happening with my Roblox
  2. 0:02character right now wait oh my goodness
  3. 0:05look at this effect on my character
  4. 0:07right now oh my God it is very
  5. 0:09performance heavy but it it's such a
  6. 0:11cool looking effect oh my goodness the
  7. 0:14only way I can see this working is if we
  8. 0:16used a service that basically allows us
  9. 0:18to create this sort of frame every
  10. 0:20single millisecond that's in a loop okay
  11. 0:23but to tell you the truth I do know what
  12. 0:24service is causing this to happen and
  13. 0:26it's called run service that basically
  14. 0:28allows us to have time management
  15. 0:30capabilities and also be able to know
  16. 0:33about script contexts and just to
  17. 0:34understand more about roblox's Loop by
  18. 0:37Loop structure if you don't know what
  19. 0:39any of that means right now don't worry
  20. 0:40I'm going to go over what all that means
  21. 0:42so that I can help you understand what
  22. 0:44is happening here so with that being
  23. 0:45said I'm going to go a little more in
  24. 0:47depth to help you understand more about
  25. 0:48run service before we incorporate it
  26. 0:50inside of Roblox studio all right let's
  27. 0:52think about this for a minute I'm going
  28. 0:53to try and explain to you a little bit
  29. 0:55of how run service is structured so that
  30. 0:58we have a better understanding of it and
  31. 1:00how we can incorporate this inside of
  32. 1:01Roblox Studio okay so in the past we've
  33. 1:04worked with managing tasks in how they
  34. 1:07execute and when they execute within a
  35. 1:09Roblox game we've done we've done things
  36. 1:12like task functions with task. weight
  37. 1:15task. delay that basically determines
  38. 1:17when tasks are being executed within a
  39. 1:19script we've also done the same thing
  40. 1:21with coroutines that basically act
  41. 1:23similar uh in that regard run service
  42. 1:26basically gives us a better
  43. 1:27understanding of this hierarchy that
  44. 1:29happen happens within a task scheduler
  45. 1:32and it's important to know uh what tasks
  46. 1:35are executed before other ones so that
  47. 1:38we can incorporate our own custom tasks
  48. 1:41um to execute in this hierarchy as well
  49. 1:45and that is essentially what run service
  50. 1:47allows us to do so I want to now show
  51. 1:50you this concept of roblox's frame by
  52. 1:53frame Loop structure as we can see right
  53. 1:55over here so we have a frame happening
  54. 1:57over here and then we have uh one that's
  55. 2:00happening at the end here that's
  56. 2:01basically just going to loop back to the
  57. 2:03beginning and it's just going to
  58. 2:04continue executing what does all this
  59. 2:07mean well first we have to ask what is a
  60. 2:10frame a frame is basically a unit of
  61. 2:14game logic that tells us how much work
  62. 2:16is being done within a specific frame
  63. 2:19now I'm not going to lie to you a
  64. 2:21frame a frame executes so quickly that
  65. 2:25you won't even realize that a frame has
  66. 2:28been executed because this is where the
  67. 2:30concept frames per second come in like
  68. 2:32you know FPS so when we run things that
  69. 2:35are 30 frames per second or 60 frames
  70. 2:38per second the more frames that are
  71. 2:39executing per second the faster quote
  72. 2:42unquote work is being done because we
  73. 2:44have more frames to work with when it
  74. 2:47comes to um how much work is being done
  75. 2:49within a specific time frame of usually
  76. 2:52per second basis so when we run tasks
  77. 2:56tasks are being ran within each frame
  78. 2:59and it goes from one frame to the next
  79. 3:01frame to the next frame and then to the
  80. 3:02next frame and it's just going to keep
  81. 3:04on going and it's going to be recorded
  82. 3:06every single second that's going to be
  83. 3:07happening um within each frame a frame
  84. 3:11literally passes by every single like
  85. 3:13half a millisecond so you you won't even
  86. 3:15realize it happening and so with this
  87. 3:17loop by- loop structure this is how we
  88. 3:19are able to record how much uh how much
  89. 3:22work is being done per time basis so now
  90. 3:25that we have that understanding we need
  91. 3:27to understand how tasks are organ
  92. 3:29organized and executed based on their
  93. 3:31priority levels so let's say with our
  94. 3:34task Schuler so our task Scher has all
  95. 3:38these different types of um I guess
  96. 3:41priorities when it comes to what task is
  97. 3:43being executed first and which ones are
  98. 3:45executed last so the ones that are
  99. 3:48executed in purple these are events that
  100. 3:51we can actually use uh with our own
  101. 3:54tasks with this hierarchy that's
  102. 3:56happening here so uh the one at the very
  103. 3:59top this is basically what's being ex
  104. 4:01executed first so we have the user input
  105. 4:04and then we go down to this list so with
  106. 4:06things like rendering weight States
  107. 4:08garbage collection physics simulation
  108. 4:10and then heartbeat so obviously we know
  109. 4:12what user input is it's basically how we
  110. 4:14detect uh input coming from the player
  111. 4:17so if I'm moving my character it's
  112. 4:19detecting the keys on my player and then
  113. 4:22it goes onto this rendering stage that
  114. 4:25basically shows that you know this is
  115. 4:28tasks that these are tasks T that are
  116. 4:30being run before the frame is even shown
  117. 4:32into the 3D World and then we have other
  118. 4:34things like weight States so these are
  119. 4:36the task functions that we've worked
  120. 4:37with in the past and then we have things
  121. 4:39like physics simulation so basically uh
  122. 4:42any work that's being done in the 3D
  123. 4:44workspace with the with physics and all
  124. 4:46that um and then heartbeat which is its
  125. 4:48own separate thing that we can actually
  126. 4:50work with in this hierarchy so these are
  127. 4:53things I want you understand so with
  128. 4:55this hierarchy this is all within the
  129. 4:58task scheduler hierarchy and this task
  130. 5:01scheduler is basically thrown in into
  131. 5:04each and every single frame uh that's
  132. 5:07being recorded So with this
  133. 5:09understanding of the frame by frame Loop
  134. 5:12and also this task scheduler um
  135. 5:15hierarchy we're able to get a really
  136. 5:17good understanding of how tasks are
  137. 5:20being executed within Roblox and how we
  138. 5:22can incorporate our own custom tasks uh
  139. 5:25within this hierarchy and that is
  140. 5:27essentially how this structure works
  141. 5:30with run service I hope this makes sense
  142. 5:33to you because with this understanding
  143. 5:35now we're going to be incorporating our
  144. 5:37own customized tasks in this hierarchy
  145. 5:40with this frame by frame Loop structure
  146. 5:41so with that being said let's go ahead
  147. 5:43and go straight into implementing our
  148. 5:45own tasks within this hierarchy that we
  149. 5:48have going on over here now that we
  150. 5:50understand it let's incorporate run
  151. 5:52service inside of Roblox studio so I
  152. 5:54hope my analogy made sense to you when
  153. 5:56it comes to the frame by frame Loop
  154. 5:58structure and also the way that the task
  155. 6:00schedule or hierarchy goes when it comes
  156. 6:02to what tasks are done first versus
  157. 6:04which ones are done last so the way this
  158. 6:06is going to work is we're going to
  159. 6:08create our own custom tasks and execute
  160. 6:11them using these events that are later
  161. 6:14that are colored in purple so we're
  162. 6:16going to start at the very bottom which
  163. 6:18is going to be the slowest but also the
  164. 6:20most commonly used one called heartbeat
  165. 6:23then we're going to step up to uh
  166. 6:25stepped and then finally render stepped
  167. 6:26all the way up here so what so the thing
  168. 6:29that's in common with all three of these
  169. 6:31events is that they execute tasks very
  170. 6:34very quickly and it's based on the
  171. 6:36user's Performance Based on the frames
  172. 6:38per second uh loop structure that we had
  173. 6:40going on up here so heartbeat is the
  174. 6:43slowest one but it's also the most
  175. 6:45commonly used one so that's what I'm
  176. 6:46going to show you um right now so here's
  177. 6:49what we're going to do we're going to go
  178. 6:50to model and we're going to insert a
  179. 6:53part essentially what we're going to do
  180. 6:56is move this part along the xais
  181. 7:00to constantly move in the negative
  182. 7:02Direction over here so that we can see
  183. 7:04it happen in real time once we actually
  184. 7:06run the script so we're just going to um
  185. 7:10select this we're going to rename this
  186. 7:12to heartbeat part and then we're going
  187. 7:16to insert a script inside of server
  188. 7:17script service so we're going to hit the
  189. 7:19plus sign insert a script like this
  190. 7:21we're first going to make reference to
  191. 7:22the heartbeat part we're going to say
  192. 7:24local heartbeat part equals workspace
  193. 7:27wait for child heartbe part and um more
  194. 7:32importantly we have to make sure that we
  195. 7:34actually have the Run service service
  196. 7:36declared so we're going to say local run
  197. 7:39service equals game colon get
  198. 7:42service open close parentheses run
  199. 7:45service just like this okay so here's
  200. 7:48how we run our heartbeat event we're
  201. 7:51just simply going to say run
  202. 7:53service.
  203. 7:55heartbeat colon connect function open
  204. 7:58and close pares there is one parameter
  205. 8:01here it's called Delta time I'm going to
  206. 8:03go over what this is in just a little
  207. 8:04bit but this is the basic structure of
  208. 8:07how we have our heartbeat event because
  209. 8:09this is how we connect an event just
  210. 8:10like this like we we pretty much know
  211. 8:12this and so next what we're going to do
  212. 8:13is say that every single time that this
  213. 8:15is running we're going to change the
  214. 8:17position of the heartbeat part so
  215. 8:19heartbeat part. position equals
  216. 8:22heartbeat part.
  217. 8:25position plus Vector 3. new open and
  218. 8:29close parentheses we're going to put in
  219. 8:32a -1 comma 0 comma 0 because1 on the
  220. 8:36xaxis will allow this part to go in this
  221. 8:39direction like this so if we now go into
  222. 8:41the game hit test and hit run then what
  223. 8:44we should see with the part is that it's
  224. 8:46going to move uh one stud in that
  225. 8:49direction very very quickly now once
  226. 8:51again uh heartbeat is running every
  227. 8:54single frame so like I mentioned here
  228. 8:56before so every single time a frame is
  229. 8:58is being made then it's going to move
  230. 9:00the part negative-1 in the uh xaxis and
  231. 9:03it's going to do it again and again and
  232. 9:04again and again and it's going to run
  233. 9:06very very fast like uh if I were to
  234. 9:09visualize how fast this is we can do a
  235. 9:11print here just by saying heart uh beat
  236. 9:14like this and then we go into the game
  237. 9:17hit run if we look at the output we can
  238. 9:19see that it's literally firing like
  239. 9:21hundreds of times like this is how fast
  240. 9:24heartbeat runs it's it's actually really
  241. 9:26insane how fast heartbeat does run and
  242. 9:29keep in mind this is at the very lowest
  243. 9:32tier of the task scheduler so if we go
  244. 9:34up here it's going to run even faster
  245. 9:36than the one down here which is actually
  246. 9:38pretty insane so like I mentioned uh the
  247. 9:40significance of heartbeat is that it's
  248. 9:42the slowest but it's also the most
  249. 9:44commonly used one and the reason we use
  250. 9:46heartbeat out of all these other ones is
  251. 9:47because this comes after physics
  252. 9:50simulation so basically like if we have
  253. 9:52our player that's already running that
  254. 9:53we already had our user input down for
  255. 9:56then uh heartbeat is going to be
  256. 9:58everything that comes after this so
  257. 10:00that's why we use this one out of all
  258. 10:02these other ones now here's some tips I
  259. 10:04have for you when it comes to using this
  260. 10:06and you know why we should use it so if
  261. 10:07you were to go back to our script you
  262. 10:09might think why don't we just use a
  263. 10:11while task. weight do like this and then
  264. 10:15just copy everything that's that's in
  265. 10:17here inside of here we could do this
  266. 10:19instead of using heartbeat but once
  267. 10:21again if you imagine where heartbeat is
  268. 10:24inside of this task scheduler um
  269. 10:26hierarchy heartbeat is all the way down
  270. 10:28here but then we can consider what's
  271. 10:29going to be all the way back here with
  272. 10:31weight states with task. we task. spawn
  273. 10:33delay so you might ask like you know why
  274. 10:36why is there a difference like why is
  275. 10:37there a significance when it comes to
  276. 10:39this let's imagine this in terms of how
  277. 10:42the script is handling what's happening
  278. 10:44here if we were to use a task. weight
  279. 10:47for this wild true do statement we are
  280. 10:49literally pausing execution of the
  281. 10:51script before we go in and try to ex
  282. 10:55whatever is going on down here and this
  283. 10:57is a fixed length keep in mind as well
  284. 10:59so Tas task. weight with nothing in here
  285. 11:02could be like
  286. 11:040.1 this is going to be a fixed number
  287. 11:06throughout the entirety of the game's
  288. 11:08duration but if let's say one of the
  289. 11:10players had bad performance issues with
  290. 11:13their device where um the frames for
  291. 11:15them run slower than if we had a in
  292. 11:19comparison to a gaming PC that can run
  293. 11:22really fast this can run really fast
  294. 11:24versus somebody who can run very slow
  295. 11:27with like their iPhone 4 or something
  296. 11:29all of these frames really depend on the
  297. 11:31performance and that's why run service
  298. 11:33matters so much when it comes to when
  299. 11:36tasks are being executed because it's
  300. 11:37primarily based on the users's
  301. 11:40performance of the device that they're
  302. 11:42playing on so that's why instead of this
  303. 11:44fixed length um if we were to let's say
  304. 11:47be playing on a mobile phone then it
  305. 11:49would drop down to this versus what it
  306. 11:51would be like if we were playing on a
  307. 11:52gaming PC like this it really depends on
  308. 11:56what device the player is playing on so
  309. 11:58that is one of the the so that is a
  310. 12:00really big difference when it comes to
  311. 12:02uh heartbeat over using using like a
  312. 12:05task. w while loop like this so here's
  313. 12:07another tip I have for you I wouldn't
  314. 12:09encourage you to use heartbeat or any of
  315. 12:11these other run uh events uh throughout
  316. 12:14the entire duration of the game I think
  317. 12:16it's better for you to wrap this in a
  318. 12:17temporary connection and then disconnect
  319. 12:20it whenever you don't need to have it
  320. 12:22anymore so here's um so here's an
  321. 12:24example if I were to wrap this uh
  322. 12:27connection inside of a local variable by
  323. 12:29saying uh local connection equals run
  324. 12:32service heartbe connect function so so
  325. 12:34as soon as we run the script we're going
  326. 12:36to have a variable be set equal to this
  327. 12:39connection right here and it's
  328. 12:40automatically going to fire so then if
  329. 12:42we were to let's say have a task. weight
  330. 12:44for about five seconds over here then it
  331. 12:46we're literally going to disconnect this
  332. 12:49connection so that we can stop heartbeat
  333. 12:51from continuing after we don't need it
  334. 12:53for a while so we're just simply going
  335. 12:55to say connection colon disconnect like
  336. 12:59this like it literally disconnects the
  337. 13:01connection that we've created over here
  338. 13:04by placing this inside of a variable so
  339. 13:07what happens now is if we hit run and
  340. 13:09then we see this change happen with the
  341. 13:11part moving so fast in that direction
  342. 13:14for about 5 seconds it's literally just
  343. 13:16going to stop right there because we
  344. 13:17waited 5 Seconds um the connection for
  345. 13:20heartbeat literally stopped after we
  346. 13:22decided to hit the disconnect on that
  347. 13:25connection so this is very effective if
  348. 13:27you want to temporarily have uh events
  349. 13:30be connected and you can disable them if
  350. 13:33you don't want that connection
  351. 13:34continuing anymore after that so that's
  352. 13:36another tip I have for you and I hope
  353. 13:38you find this to be useful okay but now
  354. 13:40going back into our heartbeat function
  355. 13:42there is this parameter called Delta
  356. 13:43time and you might ask what is this
  357. 13:46thing being passed into the function
  358. 13:48Delta time basically tells us how much
  359. 13:51time has elapsed from the current time
  360. 13:54versus the time that it took to start
  361. 13:56the last frame so if we were to go back
  362. 13:58into our script so basically whenever
  363. 14:00this Frame fires uh it's going to detect
  364. 14:03how much time has passed from when this
  365. 14:05Frame started versus when this Frame
  366. 14:08started and so this basically gives us a
  367. 14:10good uh indicator of how much time has
  368. 14:12passed in between these two frames and
  369. 14:14this is useful for making precise
  370. 14:17calculations based on the performance of
  371. 14:19The Player's uh uh device and a really
  372. 14:22good example I can think of this is if I
  373. 14:24had let's say A Part here that rotated
  374. 14:27based on the player's device performance
  375. 14:29so if we were to let's say smoothly uh
  376. 14:32rotate this part like let's just imagine
  377. 14:33this part was smoothly rotating it's
  378. 14:35going to either move slowly or fast
  379. 14:38depending on the player's performance
  380. 14:40and this is very useful for optimizing
  381. 14:42the player's experience depending on
  382. 14:44their devic's performance so that's a
  383. 14:46really good example I can think of but
  384. 14:48we can incorporate this inside of our
  385. 14:50script as well and I'm going to show you
  386. 14:51how we can do this so instead of having
  387. 14:55a fixed Vector 3 value with um negative
  388. 14:58-1 each time time the position is being
  389. 15:00changed what we can do instead is create
  390. 15:03a local variable called distance and we
  391. 15:05can set this equal to Delta time and
  392. 15:08then we can have like a multiplier here
  393. 15:10just by I don't know saying like 10 in
  394. 15:12this case and then we can replace the
  395. 15:14ne1 here which is a fixed number to
  396. 15:17distance and I'm also going to make this
  397. 15:20multiply by -10 so that it goes in the
  398. 15:22negative Direction okay so now that we
  399. 15:24have this let's go into the game and hit
  400. 15:27run and see what happens so now now that
  401. 15:29we have this the part is moving slower
  402. 15:32than it should be oh well it's actually
  403. 15:33like in the way of these frames my bad
  404. 15:36so let me actually just move all of
  405. 15:38these really quick and then hit run
  406. 15:40again so now what we should see is this
  407. 15:43part is constantly going to be moving uh
  408. 15:45in this direction until it just stops
  409. 15:47after like 5 seconds because it's now
  410. 15:50dependent on the player's performance if
  411. 15:52the player was uh having a slower device
  412. 15:54then this part would have moved even
  413. 15:56slower than it should have versus um a
  414. 15:59faster versus a faster device than this
  415. 16:02part would be moving faster because of
  416. 16:03it and um if we want to just quickly
  417. 16:05measure like what the Delta time
  418. 16:07actually is um we can just print this
  419. 16:09out in the output so that we can see uh
  420. 16:11how much time is being elapsed uh before
  421. 16:14the other frame so as you can see it's a
  422. 16:16very small number if um if we decide to
  423. 16:19use heartbeat over anything else so
  424. 16:22those are a few optimization tips that I
  425. 16:24have for heartbeat and also the
  426. 16:27explanation of using Delta time I hope
  427. 16:29this part has made sense to you so far
  428. 16:31up until this point okay so the next
  429. 16:33event is stepped and this comes in
  430. 16:35before the physics simulation and so the
  431. 16:39main difference between stepped and
  432. 16:41heartbeat is that stepped is executed
  433. 16:43before all of the physics in the game is
  434. 16:45being executed while heartbeat is
  435. 16:48executed after all the physics
  436. 16:50simulations have been made so this tells
  437. 16:52us that if we want to use stepped this
  438. 16:55would primarily be used if we want to do
  439. 16:57something related to phys physics before
  440. 17:00executing the actual physics simulation
  441. 17:02of our 3D World so in this case if we
  442. 17:04wanted to make changes to like let's say
  443. 17:06A C frame of a part or if we wanted to
  444. 17:08move the velocity or just like other
  445. 17:10things like that we would use stepped
  446. 17:12over heartbeat and another added bonus
  447. 17:14to steep is that this executes before
  448. 17:16heartbeat so this executes more
  449. 17:19frequently than heartbeat at a faster
  450. 17:21rate so this one works even faster than
  451. 17:24heartbeat in a basic sense so let's do a
  452. 17:28side by-side comparison with another
  453. 17:30part in relation to heartbeat part so
  454. 17:32I'm going to duplicate this heartbeat
  455. 17:35part and move it over here I'm also
  456. 17:36going to uh change the colors of these
  457. 17:39so that we can differentiate between the
  458. 17:40two um just like this and I'm going to
  459. 17:43call this uh stepped part so now what
  460. 17:48we're going to do is go back into our
  461. 17:49script we're going to just simply copy
  462. 17:52this code right here and then paste it
  463. 17:54down here we're going to call this
  464. 17:56connection connection 2 and then we're
  465. 17:58going to disconnect connection 2 after 5
  466. 18:01Seconds just like we did with the first
  467. 18:03connection just like this and then
  468. 18:06instead of heartbeat we're going to
  469. 18:08replace this with Ste now what's
  470. 18:10interesting about Ste is that this
  471. 18:13function gives two arguments so not only
  472. 18:15does it give the Delta time but but it
  473. 18:17also gives the time I'm just going to
  474. 18:19call this duration time because time is
  475. 18:20a reserved keyword based on roblox's
  476. 18:22standards so this time basically tells
  477. 18:25us that uh this is how much time has
  478. 18:28elapsed ever since the game started but
  479. 18:30this is how much time has elapsed ever
  480. 18:32since the last frame was executed so
  481. 18:35there is a difference between the two
  482. 18:37and you need to make sure that you do
  483. 18:38know when to use either one of these
  484. 18:40things now generally you don't really
  485. 18:42need time but I would still say Delta
  486. 18:44time is pretty important uh I'm also
  487. 18:46going to make reference to the stepped
  488. 18:49part so I'm going to copy this paste it
  489. 18:50like this and then say stepped part wait
  490. 18:54for child stepped part I'm going to copy
  491. 18:57this and I'm going to find every
  492. 18:59instance of heartbeat part and replace
  493. 19:01it with stepped part instead just like
  494. 19:03this okay so now that we have this taken
  495. 19:05care of let's go and do a race we're
  496. 19:07going to do a test comparison of how
  497. 19:09much time will uh elapse with this
  498. 19:13happening okay so as you can see
  499. 19:15heartbeat is running faster but what
  500. 19:17does this tell us I thought uh stepped
  501. 19:19was supposed to be faster the reason
  502. 19:21this is happening is because stepped
  503. 19:24executes a lot faster than than
  504. 19:26heartbeat does and whenever we are
  505. 19:29recording the distance we are using the
  506. 19:30Delta time from the current frame to the
  507. 19:33last frame and so the distance is going
  508. 19:36to generally be smaller than heartbeat
  509. 19:39but we're going to be using but we're
  510. 19:42going to be executing this position
  511. 19:44change more frequently than this one so
  512. 19:47as a result heartbeat uh executes less
  513. 19:51but it has a bigger distance but stepped
  514. 19:54has uh more execution but has a smaller
  515. 19:57distance each time we executed which is
  516. 19:59really interesting so in essence when it
  517. 20:02comes to knowing which one is faster
  518. 20:04there's a lot of variables that tie into
  519. 20:06it and there's a lot of definitions when
  520. 20:08it comes to which one is quote unquote
  521. 20:10faster so that's why you need to
  522. 20:12understand this from the perspective of
  523. 20:14the hierarchy of like do you need to
  524. 20:17make calculations before the physics is
  525. 20:19actually running or do you need to make
  526. 20:21calculations after the physics is
  527. 20:22running I would say this is what's more
  528. 20:24important uh when you're trying to
  529. 20:26decide which one of the two you need to
  530. 20:27use but if worse comes to worse if you
  531. 20:30don't know which one to use just use
  532. 20:32heartbeat like there's nothing wrong
  533. 20:33with using heartbeat uh this is going to
  534. 20:35be basically for more uh specific cases
  535. 20:39if you do need to use them so that is
  536. 20:41hopefully an analogy that helped you
  537. 20:43understand the difference between
  538. 20:44stepped and heartbeat if you decide to
  539. 20:46use either one of these in the future so
  540. 20:48we finally reached the last event on
  541. 20:50this hierarchy called render stepped and
  542. 20:52as you can see this is all the way up
  543. 20:54here in the hierarchy as being part of
  544. 20:57the rendering stage of this task
  545. 20:59scheduling um list so when we use render
  546. 21:03you can run stuff very fast with this
  547. 21:05thing like at a very very frequent rate
  548. 21:07but there's a few things that we need to
  549. 21:09keep in mind about render so the first
  550. 21:12thing that's really interesting is that
  551. 21:13we can only use render Ste on the local
  552. 21:16side we can't use this on the server
  553. 21:18side like we've been doing with
  554. 21:20heartbeat and Ste this can only be done
  555. 21:22by the local player so that is one very
  556. 21:24important distinction to know about when
  557. 21:26it comes to using render step is we can
  558. 21:28only use it on the local side and so
  559. 21:30with that being said I'm going to show
  560. 21:31you a demonstration of using render
  561. 21:34using gys rather than using parts to
  562. 21:37move them around using physics and all
  563. 21:38that sort of stuff so what I'm going to
  564. 21:40do is go to starter GUI hit the plus
  565. 21:43sign create a screen GUI I'm also going
  566. 21:46to create a a text label like this uh so
  567. 21:49that we can see it on our screen and
  568. 21:51then I'm just going to align it like
  569. 21:53right here like so and then I'm going to
  570. 21:55insert a local script inside of the text
  571. 21:57label so the first thing I'm going to do
  572. 21:59is make reference to the text label
  573. 22:02equals script. parent and I'm also going
  574. 22:05to take the Run service declaration
  575. 22:08paste that into here and then I'm also
  576. 22:10going to take this task. weight
  577. 22:13statement and also this connection
  578. 22:14disconnect um statement right here okay
  579. 22:17so now what we're going to do is right
  580. 22:19before the task. weight statement we're
  581. 22:20going to make a variable called local
  582. 22:24connection equals run
  583. 22:26service do render stepped colon connect
  584. 22:32function open and close parenthesis
  585. 22:34enter now this one only takes in one
  586. 22:37argument which is going to be the Delta
  587. 22:39time which is very similar to to our
  588. 22:42heartbeat connection rather than our Ste
  589. 22:45connection which has two parameters in
  590. 22:46there so now I'm just going to Simply
  591. 22:48calculate a um distance I guess we could
  592. 22:51just say local distance equals Delta
  593. 22:54time time 100 like this and so what do
  594. 22:58we want to do with this distance well
  595. 23:00what we can do we can basically rotate
  596. 23:03this part clockwise based on the
  597. 23:04player's performance so that we know
  598. 23:06that render step is working as expected
  599. 23:08so first thing we can do is probably
  600. 23:10just make a print statement here by
  601. 23:12saying render St like this so that we
  602. 23:14actually know how many times this is
  603. 23:16being run and then what we can do is say
  604. 23:19text label. rotation equals text label.
  605. 23:24rotation plus distance like this all
  606. 23:28right so in order to test this we're
  607. 23:30going to have to go into the game hit
  608. 23:32test and then we have to hit play so
  609. 23:34that our actual character is going to be
  610. 23:36inside of the game all right so now that
  611. 23:38we have this we can see that it's
  612. 23:40constantly rotating uh from where it
  613. 23:42started uh to constantly just go
  614. 23:45clockwise like this until about 10
  615. 23:47seconds where we just disconnect the
  616. 23:48connection just like that but as you can
  617. 23:50see within this uh 10-second time frame
  618. 23:52uh it ran um very very frequently like I
  619. 23:56can't even describe how frequently
  620. 23:58render Ste executed but it it executed
  621. 24:01very very frequently at at least I can
  622. 24:03tell you that faster than what Ste could
  623. 24:05execute faster than what heartbeat could
  624. 24:07execute but once again this can only be
  625. 24:09executed on the local side so there are
  626. 24:12trade-offs to using render step but I
  627. 24:14recommend trying to experiment and
  628. 24:16figure out which ones work best for you
  629. 24:18when it comes to when it comes to
  630. 24:20knowing which event to use because
  631. 24:22render it's all the way up here in the
  632. 24:24rendering process so you can decide for
  633. 24:26yourself whether this is the right step
  634. 24:28for you to use in your scripts or if you
  635. 24:31just want to use stepped or heartbeat
  636. 24:32like it honestly doesn't really matter
  637. 24:34too much of a difference which one you
  638. 24:35use but it's still good to understand
  639. 24:38why there is a difference between these
  640. 24:40three and how this task scheduling
  641. 24:41hierarchy works so I hope this part has
  642. 24:44made sense to you up until this point so
  643. 24:46we've pretty much gotten over the basics
  644. 24:48of time management when it comes to run
  645. 24:50service and what it has to offer for
  646. 24:53executing tasks at specific times during
  647. 24:56specific occasions that we'd want uh to
  648. 24:58execute them but run service has
  649. 25:00something else that we uh May deem to be
  650. 25:03useful and that is knowing the context
  651. 25:06of scripts being executed and what I'm
  652. 25:08trying to get out here is run service
  653. 25:10has properties to tell us if let's say
  654. 25:12we're playing the game in studio or if
  655. 25:14this script is being run on the server
  656. 25:16or if the script is being run on the
  657. 25:17client we can tell based on the context
  658. 25:20of where the script is being executed
  659. 25:21and if that's the case then we can
  660. 25:23perform specific tasks based on specific
  661. 25:27criteria like that uh an example I can
  662. 25:30think of is if let's say I was
  663. 25:32programming a boss battle uh and it has
  664. 25:34cutscenes and everything like that then
  665. 25:36i' probably just want to skip the
  666. 25:38cutscenes if I'm playing inside of
  667. 25:40Roblox studio just so that I can test
  668. 25:42the boss um and that's just one example
  669. 25:45I can there's so many examples you can
  670. 25:46probably think of if you want to do
  671. 25:48something specific with your scripts if
  672. 25:50you're playing in Roblox Studio versus
  673. 25:52if you're playing in the actual game and
  674. 25:54we can do the same thing with knowing if
  675. 25:56a piece of code in a script is being
  676. 25:58executed on the server versus if it's
  677. 26:00being executed on the client and this is
  678. 26:03also very useful for module scripts
  679. 26:05which I'm going to show you in just a
  680. 26:06little bit so for the rest of this video
  681. 26:08I'm going to show you these properties
  682. 26:10that we can use inside of Roblox studio
  683. 26:12so let's go ahead and go straight into
  684. 26:13that all right so let's start with
  685. 26:14something easy which is basically
  686. 26:16determining if we are playing the game
  687. 26:18inside of Roblox Studio or not so here's
  688. 26:20how it's going to work I'm going to go
  689. 26:22on the right side and just very quickly
  690. 26:24create a new script I'm going to call
  691. 26:26this script uh context like this and
  692. 26:29then what we're going to do is first
  693. 26:31make declaration to run service I can
  694. 26:32just go to one of my previous scripts
  695. 26:34and duplicate that place it into here
  696. 26:36and then here's all we need to say we
  697. 26:38can just say if run service colon is
  698. 26:43Studio open and close parentheses then
  699. 26:46print player is playing in studio and
  700. 26:51then if we don't do that then we can say
  701. 26:54player is not playing in studio
  702. 26:58so it seems very easy right we just go
  703. 27:00into the game hit test hit play and then
  704. 27:02on the output what we should see okay
  705. 27:04well if we uh if we just disabled our
  706. 27:07previous script for for for a minute
  707. 27:10let's go back into the game and then we
  708. 27:11should see that player is playing inside
  709. 27:14of Studio because we are indeed playing
  710. 27:16inside of studio if you were playing in
  711. 27:18an actual game then it's just going to
  712. 27:20go to the L statement and then go down
  713. 27:21here by saying player is not playing in
  714. 27:23studio and not only do we have checks
  715. 27:25for if the player's in studio we can
  716. 27:27also check if the player is I don't know
  717. 27:29play testing or if they're in run mode
  718. 27:32uh all of these different things we we
  719. 27:33can check for all of these different
  720. 27:35contexts if we were to have them inside
  721. 27:37of a script uh to perform different
  722. 27:39things based on the script context so
  723. 27:41that's one batch of these functions that
  724. 27:43I'm going to show you here and it seems
  725. 27:44to be very self-explanatory it's pretty
  726. 27:46easy okay so now I'm going to show you
  727. 27:47some other ones like is server and is
  728. 27:51client now if you've been scripting for
  729. 27:53a very long time and you've been
  730. 27:54following the series I think you know
  731. 27:55the difference between the server and
  732. 27:57client is I don't need to explain this
  733. 28:00um but we can basically check in a
  734. 28:01single script if we are playing on the
  735. 28:04server or if we are playing on the
  736. 28:05client based on the script's execution
  737. 28:08and uh which context we are executing
  738. 28:11this script from so kind of like what we
  739. 28:13had before up here we could just say if
  740. 28:15run Service uh colon is server then we
  741. 28:20can print that we are on the server uh
  742. 28:22if we say if run service is client then
  743. 28:27we can print or just like do whatever is
  744. 28:29going to be here if we are playing on
  745. 28:31the client so both of these are being
  746. 28:33checked inside of the context of this
  747. 28:36script inside of serers script service
  748. 28:38you kind of have to think outside the
  749. 28:39box a little bit to really understand
  750. 28:41and think about when this would be
  751. 28:44useful like when would you want to check
  752. 28:47if this uh execution is happening on the
  753. 28:49server or if this is happening on the
  754. 28:51client I'm going to show you one example
  755. 28:53of using this that I find to be pretty
  756. 28:55helpful so I'm just going to delete this
  757. 28:56code that we have over here and what I'm
  758. 28:58going to do is create a module script on
  759. 29:00the right side of replicated storage so
  760. 29:02I'm going to hit the plus sign I'm going
  761. 29:03to create a module script right over
  762. 29:05here and basically the point of this
  763. 29:07module script is I'm going to create a
  764. 29:09function and this is going to check if
  765. 29:12uh wherever this function was executed
  766. 29:14from whether that was from the server or
  767. 29:16the client are we going to continue with
  768. 29:18the execution of this function so I'm
  769. 29:20going to show you what this looks like
  770. 29:21I'm going to say function module do uh
  771. 29:24we could just say like my function or
  772. 29:26something like that open close print es
  773. 29:28enter so what we're going to do is get
  774. 29:32reference to our run service I'm just
  775. 29:33going to copy this paste this up here
  776. 29:35I'm first going to say if run
  777. 29:39Service uh colon is client open close
  778. 29:42parentheses then we're just simply going
  779. 29:44to return this and then uh we're going
  780. 29:47to make a print statement down here by
  781. 29:49saying um my function has been executed
  782. 29:54and you can just like have whatever
  783. 29:56whatever else is happening down here so
  784. 29:57let's try understand what's happening
  785. 29:59here when we run my function it's first
  786. 30:01going to check if whatever called this
  787. 30:04function is on the client then we're
  788. 30:05just going to return it because let's
  789. 30:07say a hacker is uh trying to execute
  790. 30:10that they see inside of this module
  791. 30:11script well since they're on the client
  792. 30:13if they try to activate this function
  793. 30:16and it sees that it's being activated
  794. 30:19from the client then we're just not
  795. 30:20going to do anything about it we're just
  796. 30:21simply going to return it but if we are
  797. 30:24executing this on the server then we're
  798. 30:26going to drop down here and then
  799. 30:27continue off with whatever we were
  800. 30:29trying to do with our function just
  801. 30:30execute it like normal so now I'm going
  802. 30:32to show you what this looks like if we
  803. 30:34were to uh require this uh module script
  804. 30:37and try to execute it so we're going to
  805. 30:38drop two lines down here and say local
  806. 30:41module equals
  807. 30:43require game. replicated
  808. 30:47storage. module script like this and
  809. 30:50then all we're going to do is just say
  810. 30:52module. my function open and close
  811. 30:54parentheses just like that so we are
  812. 30:56running this on the server so if we go
  813. 30:58into the game hit test and hit play then
  814. 31:00what we should see in the output is that
  815. 31:02my function has been executed because we
  816. 31:05are playing on the perspective of well
  817. 31:07okay we're playing in the perspective of
  818. 31:09the the player but we can see down here
  819. 31:11highlighted in green that means this was
  820. 31:13printed on the server so my function has
  821. 31:15been executed by the server as expected
  822. 31:18so now let's see what happens if we
  823. 31:20decide to change is client to is server
  824. 31:23instead so that means this can only be
  825. 31:26executed on the client
  826. 31:28so if we go into the output we can see
  827. 31:30that player is playing in studio but we
  828. 31:33do not see the print statement for uh my
  829. 31:36function has been executed because we
  830. 31:38executed this function on the server so
  831. 31:42because we are now uh seeing if we're
  832. 31:44trying to be on the client it's just not
  833. 31:46going to run and um if we try to do this
  834. 31:49the other way around too so I'm
  835. 31:50basically just going to very quickly
  836. 31:52create a local script inside of starter
  837. 31:54player starter player scripts I'm just
  838. 31:56going to create a local script
  839. 31:58I'm going to copy this run service
  840. 32:00command here and these two down here
  841. 32:04what I'm going to do is just quickly
  842. 32:07disable my context script inside of
  843. 32:09server script service go into the game
  844. 32:10hit test and hit play so we should see
  845. 32:12in the output is absolutely nothing
  846. 32:15because we are trying to see if we are
  847. 32:18executing this on the server but if we
  848. 32:21do this the other way around with is
  849. 32:23server then what we should see in the
  850. 32:25output is finally my function has
  851. 32:28executed because we are calling this on
  852. 32:29the client and we are seeing in the
  853. 32:31module script if this is a client or if
  854. 32:34this is not a server I mean if this is
  855. 32:36uh a client then we're going to execute
  856. 32:38what's going to be down here you get the
  857. 32:40idea at this point um you kind of have
  858. 32:42to think a little bit of how you can
  859. 32:43incorporate is server or all of these
  860. 32:45other context uh functions inside of
  861. 32:48your script and so that is another
  862. 32:51really cool and neat thing you can do
  863. 32:53with run service that other services do
  864. 32:55not really provide so
  865. 32:58I hope you found this part to be uh
  866. 33:00informative as well and it can benefit
  867. 33:02you for your Roblox Development Career
  868. 33:05journey I hope you found this episode to
  869. 33:07be informative this is one of the more
  870. 33:09complicated Concepts in Roblox Ser
  871. 33:11development so if you've made it this
  872. 33:12far into the series then I could then I
  873. 33:15got to congratulate you on that cuz
  874. 33:17these Concepts they're still going to
  875. 33:19continuously become more and more
  876. 33:21difficult as time goes on within the
  877. 33:22series and you should be proud of it so
  878. 33:24to recap what just happened we learned
  879. 33:26about the frame by frame Loop structure
  880. 33:28that Roblox has we learned about the
  881. 33:30task scheduler hierarchy that happens
  882. 33:32here we learned about uh how we can
  883. 33:35incorporate our own uh how we can
  884. 33:37incorporate our own tasks into uh the
  885. 33:40task scheduler and also how we can run
  886. 33:43certain contexts uh inside of our
  887. 33:45scripts to determine um what's being
  888. 33:48executed and how we can execute the
  889. 33:50script based on the context of of where
  890. 33:52it was executed from so with everything
  891. 33:55that I've taught you in this video I
  892. 33:56hope you put it to Great use inside of
  893. 33:57your Roblox scripting and with that
  894. 33:59being said that is pretty much going to
  895. 34:01be it for this episode if you want to
  896. 34:02learn more about time management and
  897. 34:05also executing tasks during specific
  898. 34:07times within the game then I encourage
  899. 34:08you to watch my core routines video
  900. 34:10which I will have a link for you right
  901. 34:12here in this video but with that being
  902. 34:13said I will see you in the next episode
  903. 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.