YouTube2Text

Coroutines - Roblox Advanced Scripting #8 — Transcript

by BrawlDev · 6,298 words · 340 segments · language en · Watch on YouTube

Full transcript

  1. 0:00hm I didn't know there was a disco going on here  oh well I'm not going to question it I just find
  2. 0:07it to be pretty crazy that these two parts are  essentially changing colors just simultaneously at
  3. 0:12the same time especially when it's looping through  so many different parts at once you wouldn't
  4. 0:16really be able to do this inside of a standard  script unless there were multiple tasks running
  5. 0:21at the same time I think what I just explained  pretty much sums up what I'm going to be talking
  6. 0:25about in this episode which is kind of a part two  to my task functions video that I made a while
  7. 0:29ago how having the ability to do multiple tasks in  the same script is something very powerful that's
  8. 0:34pretty much with the use of core routines and it's  important to understand that there is a difference
  9. 0:38between core routines and task functions and  why I made them in two separate videos so this
  10. 0:43will pretty much be a continuation from my task  functions video into another multitasking system
  11. 0:49that is honestly better than task functions  depending on how you use it and so that's
  12. 0:53what I'm going to be showing you how to do inside  of this episode all right let's think about this
  13. 0:56for a minute you seem to really like my example  previously with task functions when I created all
  14. 1:01these tasks represented as parts so I pretty much  brought it back but this time with core routines
  15. 1:08so what's the whole idea of coroutines well pretty  much it's kind of similar to task functions in a
  16. 1:13way how we want to create multiple threads to  be able to run multiple tasks at the same time
  17. 1:19using the same script so that we can basically  do a whole bunch of stuff simultaneously without
  18. 1:25having to wait for whatever is previously in the  script to then run whatever else is underneath
  19. 1:30it so we basically have these tasks here and we  have core routine functions that relate to what
  20. 1:36we're going to be discussing about inside of this  episode so let's say we have task one and task two
  21. 1:42we want to be able to run these two tasks at the  same time but how do we do that well what we would
  22. 1:47do is instead of writing all of this inside of  one thread we can create a new thread and make it
  23. 1:54wrapped around inside of this second task we want  to do and this is a concept called multi-threading
  24. 1:59so we would basically do that by creating a new  coroutine thread and just slap this inside of
  25. 2:04task two so that we can run both of these at the  same time but just creating the task is not going
  26. 2:09to be enough we have to actually execute the task  as well by using resume to then actually execute
  27. 2:17the core routine task that we created inside of  our script so we can run task one and task two at
  28. 2:23the same time now so far this looks pretty similar  to task functions but there is one key difference
  29. 2:29between task functions and core routines is that  we have the power to pause these multi-threading
  30. 2:35tasks using a function called yield which  basically says during execution of our core
  31. 2:42routine we can literally pause the task in order  to do other things outside of this multi-thread
  32. 2:48first before going back into this task to do  whatever we want the task to do so basically
  33. 2:54if we yield this task then we're not resuming this  task anymore and in order to to make this task run
  34. 3:01again we would just simply use resume get rid of  the yield and then the task would be running just
  35. 3:07like normal and so if at any point we don't need  to run the complete version of this task anymore
  36. 3:12or just things in the script have changed then  we can just simply close the core routine task
  37. 3:18and create a new one so it basically just gets  rid of our T our core routine task that we had
  38. 3:24uh previously and so this is just a couple methods  used for core routines uh yielding is probably the
  39. 3:29big game changer for these multi-threaded um  tasks inside of your scripts and that's what
  40. 3:35we're going to be doing inside of this episode it  serves as task functions with more control over
  41. 3:40your multi-threading processes so I hope that made  sense to you and now let's get to implementing now
  42. 3:46that we understand it let's use cortines to create  multiple threads so that they can all run at the
  43. 3:51same time using the same script so on the right  side next to serers script service we're going
  44. 3:56to hit the plus sign and we're going to create  a new script before we start writing here's what
  45. 4:00I want you to understand about core routines  is that when you create a new core routine it
  46. 4:05should be placed inside of a variable and when we  check to see what this core routine actually is
  47. 4:11it's basically a thread object that has different  functions that that we're going to be using inside
  48. 4:17of this episode so let's create a variable and  this one is going to represent our second task
  49. 4:21so in double dashes we're going to make a comment  by saying task two and then down here we're going
  50. 4:27to create a coroutine for the second task so we're  going to make a variable by saying local and we're
  51. 4:33going to call this task 2 equals cortin doc create  so this is pretty much the method we're going to
  52. 4:42use in order to create a new cortine and set it  equal to our second task so in this quarantine
  53. 4:48we're going to make a function by creating one  inside of these parentheses so we're going to
  54. 4:53say function open parentheses close parentheses  hit enter so this is pretty much the general
  55. 4:58structure of creating a new cortine and saying  it equal to a variable uh if we try and print
  56. 5:05the the new cortine that we just created so let's  drop two lines and then say print task 2 if we go
  57. 5:12into the game hit test and hit run and we look at  the output we can see what kind of variable our
  58. 5:19task 2 is so it pretty much printed out a thread  object and all these numbers basically mean the
  59. 5:26location in memory that this um new object was  created in uh this part does not really matter
  60. 5:32too much but what does matter is that our cortin  that we created is basically a thread object that
  61. 5:38has functions that that can be used to manipulate  um when we run these core routine tasks so let's
  62. 5:46hit stop and let's go back to our script so now  that we know that when using cortin Create we
  63. 5:51basically create a a new thread object and we set  that equal to our task 2 variable so let's delete
  64. 5:57this print statement here and now we can write  stuff stuff inside of this new cortine that we
  65. 6:02created this new cortine task that we created so  we can just make a print statement here by saying
  66. 6:07print cortine task and then we can drop two lines  down here so there we go we've basically created
  67. 6:15our first cortine but if we try and Run the game  this task isn't actually going to run because we
  68. 6:20tied it into a variable so now we have to execute  task to and the way we're going to do that is by
  69. 6:25using a function from cortine called called resume  because by default when we create a cortine it's
  70. 6:34not going to run on its own we basically have to  do that manually so what we're going to do down
  71. 6:39here is basically write cortin do resume and  then in parentheses we're going to put in our
  72. 6:46core routine that we created which is Task two so  now if we hit run we should see our core routine
  73. 6:53uh executing so down here it says coroutine task  because we created our cortine and then we used
  74. 6:58the coroutine resume function and we passed in  our task 2 cortine to actually run it there's
  75. 7:05different states to these cortines which I will  explain later on in this episode but that's just
  76. 7:09something for you to know about for now now it  might seem kind of unusual that we're creating a
  77. 7:14task wrapped inside of a variable and then we're  just calling cortine resume on that cortine that
  78. 7:19we created it seems like a weird structure however  there is an easier way to write this and this is
  79. 7:25going to use wrapper functions so the way it's  going to look is we're going to first create
  80. 7:30a task and then we're just going to call that  task after we've created it let's make a blocked
  81. 7:36comment statement by selecting up here and we're  going to put the two dashes but also put in two
  82. 7:42open hard brackets and then at the end here we're  going to put in the closed end brackets by putting
  83. 7:47two of them here so now let's drop two lines and  what we'll do is we'll create a variable and then
  84. 7:52we will wrap this around inside of a qu routine  wrap function so the way it's going to look is
  85. 7:57we're going to first create our variable which  is local task 2 equals core routine. wrap and
  86. 8:04then inside of here we're going to create another  function open parentheses close parentheses hit
  87. 8:08enter and let's make our same print statement  so we're going to say core routine task and so
  88. 8:14right now this looks pretty similar to our first  core routine task but what we can just say down
  89. 8:19here is Task 2 and then put in the open and close  parentheses and this is basically all you need to
  90. 8:26do so it's a little more simplified than we had up  here but both of them generally do the same exact
  91. 8:32thing so if we hit run then we should see the  exact same result just like in the first example
  92. 8:37so just printed cortine task after we created  a wrapper function called that function just by
  93. 8:43its name with the open and close parentheses so  that's another way of writing it and if you want
  94. 8:47to further simplify this even more what you can  do is not even have a variable here just like get
  95. 8:54rid of the task two variable and get rid of this  down here as well and we can simply just put in
  96. 8:59two parentheses right at the end so this is all  you need to say it's just basically three lines
  97. 9:04that creates the cortine wrap function and then  it prints whatever's down here and then we end
  98. 9:09it with the open and close parentheses so this is  also another really simple way of writing these
  99. 9:14cortine threads however the issue with writing it  like this is that we're not able to retrieve this
  100. 9:20core routine once we're done executing it because  we didn't wrap this inside of a variable so I do
  101. 9:27want you to be cautious if you do write this way  there's also one more way I can think of when
  102. 9:31writing these wrapper functions inside of these  core routines and that's by wrapping a function
  103. 9:37inside of a local variable and then just using  coroutine do Wrap surrounded by that variable and
  104. 9:44just call that within one line so we're going  to make a function that's wrapped inside of
  105. 9:48a variable so we'll say local my function equals  function open parentheses close parenthesis enter
  106. 9:56and we could just put in some random stuff in here  print function and then down here what we're going
  107. 10:00to do is say cortin do wrap and then inside of  here we're going to put in our function we're
  108. 10:07going to say my function and then open parentheses  close parentheses just like this this is another
  109. 10:13really easy way to write a cortine and of course  this isn't the only way you can write a function
  110. 10:20signature you can also just say local function  my function open parentheses close parentheses
  111. 10:28and this is also another way of doing it too so  I just wanted to throw these two examples out
  112. 10:32there as well there's multiple ways to do this  stuff and it's just a matter of understanding
  113. 10:37these different structures of creating these  coroutines all right and since we are writing
  114. 10:43functions inside of these coroutines it actually  is possible to pass in function arguments and I'm
  115. 10:48going to show you this by using our last example  so let's uncomment our previous example and let's
  116. 10:56comment our new example because we will hopefully  come back to this as reference what we can do
  117. 11:01here inside of our function inside of our function  declaration is add extra parameters in here we can
  118. 11:07put whatever arguments we want in here let's make  a system where we pass in two numbers and we're
  119. 11:12going to print the sum of those two numbers so  let's make the first argument num one for number
  120. 11:18one and then comma num two for number two and then  down here what we're going to do is print num 1+
  121. 11:25num 2 and so now the way we pass in arguments into  this cortine function is by going to where we call
  122. 11:33this cortine thread and we just simply put in the  thread itself and then we space it with a comma
  123. 11:40with whatever other arguments we have after that  so this one is going to be our number one and then
  124. 11:46we put another comma here and this one's going to  be number two so once again when we call cortine
  125. 11:51resume we're first going to pass in our cortine  thread and then everything after that is going
  126. 11:58to be the function arguments our result so when  we load into the game it should say four because
  127. 12:042 + 2 is 4 and then it's going to say cortin task  so let's run the script and see our output so yeah
  128. 12:10it printed out four and it also printed out cortin  task like expected now let's try running multiple
  129. 12:15tasks at the same time so you know how two Loops  go right if it's two while Loops for instance let
  130. 12:21me just write some comments here so I can get  you to understand um this analogy so if we have
  131. 12:26two while Loops while true do and then it's going  to end with an end here and then we have another
  132. 12:33while true do statement and end it right here  basically what this is saying is that we're going
  133. 12:39to constantly go through this Loop over and over  again repeatedly and it's never going to run this
  134. 12:46while loop because it needs to finish this while  loop before going into this one but what if this
  135. 12:51while loop consistently runs throughout the entire  game without ever going to whatever is underneath
  136. 12:57here if we want to be able to run two Loops at the  same time or multiple tasks in this case we would
  137. 13:02use cortines so let's delete everything that's  inside of the script as of right now so let's
  138. 13:06delete all this and what we're going to do is  create two Loops that'll run simultaneously at the
  139. 13:12same time while the game is running in order for  this to work so what we can do is create one core
  140. 13:18routine for one of our loops and then just call  the other one normally for our first one we're
  141. 13:23going to say local Loop 1 equals cortin doc create  open parentheses close parentheses function open
  142. 13:31parentheses close parentheses enter let's create  a wall Loop let's say while true do let's print
  143. 13:37task one and then we're going to put in a task.  weight statement down here so that it doesn't just
  144. 13:44repeatedly call this uh print statement let's put  in a weight statement of 01 and what we'll do is
  145. 13:51execute this core routine by saying coroutine do  resume open parentheses close parentheses and put
  146. 13:57in Loop one so so this is our first Loop When We  Run the game then what we should see is Task one
  147. 14:03that's constantly being printed into the output  as you can see right here but what we want to do
  148. 14:07is run two two while Loops at the same time so  after our quartin resume we're basically going
  149. 14:13to do the exact same thing we did here but instead  it's going to be within the main script not inside
  150. 14:19of a thread so we're going to say while true do  enter then we're going to make a print statement
  151. 14:25by saying task two and then down here we're going  to write another task. weight statement with 0.01
  152. 14:33as our um weight counter so now if we go into  the game we should see both of these while true
  153. 14:40do statements running at the exact same time  back to back so inside of the output we can
  154. 14:46sometimes see task one printing before task two  and then task two printing before task one but
  155. 14:51generally speaking they are running at the exact  same time uh during execution so that's one of
  156. 14:58the benefits of using core routines is by being  able to run multiple tasks at the same time now
  157. 15:04you remember how I mentioned that when we create  the core routine that doesn't automatically mean
  158. 15:09that it's executing because what's happening  is that when we create a thread object using
  159. 15:14cortine Create we have to manually execute it  ourselves because these cortines have different
  160. 15:21states of operation if that makes sense so when  we create our cortine the state of the cortine is
  161. 15:28BAS basically one that's not executing so so it  hasn't started yet and these are called quartin
  162. 15:35States so there are three quartin states we should  know about if I drop a couple lines down here and
  163. 15:40just write this down inside of my script right  here well there's actually technically four but
  164. 15:44there's only three that we really need to know  about for this episode the fourth one you can
  165. 15:48do more research if you decide to go more deeper  into these quines but these are the three that we
  166. 15:53need to know about the first one is suspended the  second is running and then the third one is dead
  167. 16:00and basically what's happening is that when we  first create a quartin it's automatically going
  168. 16:05to be in the suspended State meaning that it's not  currently running but that also doesn't mean it's
  169. 16:10dead it just means it's at of pause running means  that obviously the cortine is currently running or
  170. 16:16currently executing as we speak when we check the  status and then dead pretty much just means that
  171. 16:21the cortine is either over or it got cancelled and  this cortine can no longer be used afterwards and
  172. 16:28we can check these statuses by using a function  simply called cortine do status so we can do
  173. 16:35that by dropping a line here write before calling  cortine resume we can write a print statement over
  174. 16:40here and just simply call our cortines status  by saying cortin do status and then inside of
  175. 16:49these parentheses we're going to pass in our our  coroutine Loop one and let's do the same thing
  176. 16:56after our cortin resume as well so we can check to  see the status of our quarantine in real time uh
  177. 17:04what we're going to do is actually we're going to  comment out these while true do statements so that
  178. 17:08we can see the effect of these print statements  that we're trying to look that we're trying to
  179. 17:13look for so now let's hit run and see what happens  so as you can see before our cortine executed
  180. 17:18our status of our Loop one cortine is suspended  because nothing ran yet or it's currently paused
  181. 17:26and then after our quarantine executed we checked  the status and it's now dead meaning that we can
  182. 17:32no longer run it and we're just going to have to  create a new quarantine again afterwards so that's
  183. 17:37a pretty neat thing to know about when we want to  check the status of our quartin to see whether we
  184. 17:42can either continue with our quartin or we reach  a certain point where we just don't want to use
  185. 17:47it anymore this is a great way of checking whether  that that can be done or not and finally we are at
  186. 17:53the last part that makes cortines very useful and  that is with the use of their yield function so to
  187. 18:00explain what a yield is this function basically  stops the quarantine with whatever it's doing in
  188. 18:07order to Res in order to do anything else that  the script needs to do and if we needed to we
  189. 18:13will resume the script again to to then continue  off whatever it was trying to do with this thread
  190. 18:19without completely getting rid of it let me show  you what I'm talking about let's delete all of
  191. 18:24this code that we have here and let's create a  new thread so we're going to call this one local
  192. 18:30Loop thread equals Quine doc create function open  parentheses close parentheses enter what we're
  193. 18:38going to do is make a while true do statement so  let's say while true do let's create a for Loop
  194. 18:44let's say 4 I = 1 comma 10 do so this is going  to execute 10 times whatever is inside of here
  195. 18:53we're going to write a print statement by saying  print I just simply it's just is going to print
  196. 18:58whatever the current number is here and let's also  execute this quinee so we're going to say quartin
  197. 19:04do resume openes close parentheses Loop thread  so what this should do is if we run it if we run
  198. 19:12then we should see all 10 numbers being printed  onto the output all at the same time but what if
  199. 19:19we wanted to stop execution of this core routine  to do other things afterwards and then resume this
  200. 19:27core routine again to continue what it was trying  to do before in the state that it was when we told
  201. 19:33it to pause this is where yielding comes in and  let me show you what it looks like so we're going
  202. 19:37to drop a line after the print statement and we're  going to say quartin yield open parentheses close
  203. 19:43parenthesis this is pretty much what it's going  to look like right here so what happens is that
  204. 19:48when we call resume to execute this Loop thread  quartin it's going to start at the first position
  205. 19:56of the loop it's going to print one and it's going  to pause execution of this coretine wherever it
  206. 20:02is and let me show you what that looks like if  we hit run we should only see one being printed
  207. 20:08because we told it to print one and then we told  the execution to stop now let's see what happens
  208. 20:14if we add more onto this let's drop two lines down  here and let's write a print statement here saying
  209. 20:21execution has stopped and let's drop another  line here's how we can resume the thread and you
  210. 20:27guessed it we're basically going to write another  quartin do resume and we're going to throw in the
  211. 20:33loop thread cortine now what should surprise you  is that when we call for the first quarantine
  212. 20:40and it's going to print the first number here you  might think it's going to do the same thing again
  213. 20:44for um for the print statement by just saying  print one but here's what's actually going to
  214. 20:51happen let's hit run and what should happen is  that it's first going to print one because we
  215. 20:57called execution so it's going to start at one  and it's going to and it's going to print one
  216. 21:01and it's going to pause the quarantine and then  once this is done it's going to go down here to
  217. 21:07print execution is stopped and then we come down  here to resume or continue the coroutine of loop
  218. 21:15thread it's going to pick right back up where it  left off after this coroutine yield was over it's
  219. 21:21going to go back to the top and increment one to  two and then it's going to print two and then it's
  220. 21:27going to yield again so we can just continuously  do this by saying execution is stopped and then
  221. 21:34and then it's going to resume the thread again  to print three and it's just pretty much going to
  222. 21:39keep doing this so this is going to happen exactly  four times so it's going to print one execution
  223. 21:45has stopped print two execution has stopped print  three execution has stopped and then finally it
  224. 21:48printed out four I will go into more detail with  yields but just for a quick minute I'm going to
  225. 21:54introduce one more quartin function and that's  just and that is cortine close which just simply
  226. 22:00ends the cortine and we just won't be able to use  it anytime afterwards unless we create another
  227. 22:06quarantine so what we can do is that after let's  say the second execution uh let's drop a line down
  228. 22:12here and we're going to say cortin do close and  then inside the parameters we're going to throw
  229. 22:19in our Loop thread that we created so pretty much  what's going to happen here is that when we close
  230. 22:24the quartin we're not even going to be able  to resume these threads because exit because
  231. 22:29we already closed this Loop thread so it can no  longer continue what it was trying to do so if
  232. 22:36we hit run what should happen is that after our  second execution then it pretty much printed out
  233. 22:43execution has stopped but then it tried to resume  the thread it nothing happened and then it printed
  234. 22:49out execution is stopped once again but also  it tried to resume the thread once again but it
  235. 22:54did not work because our quartin is already dead  because we called close on the cortine so that's
  236. 23:01another cortine function I want you to know about  when you're handling these cortines another thing
  237. 23:06we can do is return values when we call cortine  yield so we can actually put in parameters so we
  238. 23:12can actually put in arguments here for when we  decide to return something after we pause our
  239. 23:19cortine thread so what this will look like is this  is pretty much going to be returned when we call
  240. 23:26cortine resume because when we call cortine resume  that's when we start the thread of whatever's
  241. 23:32inside of here and then when we call cortine  yield it's going to return whatever arguments
  242. 23:37we put inside of here back to cortine resume so  we can actually wrap this cortine resume inside
  243. 23:43of a variable so before cortin resume we can say  local success equals qu. resume Loop thread and
  244. 23:51what we can decide to put in here is just put in  a Boolean here just say true basically indicating
  245. 23:57that this yield has been successful now obviously  this is kind of an arbitrary example but what I'm
  246. 24:03trying to get at here is that you can pretty  much put in any arguments here and pass it back
  247. 24:09to quarantine resume once execution is over into  our new variable so we can drop a line here and
  248. 24:16then print our our variable true if we hit run  it's first going to print the one and then it's
  249. 24:23going to print our return value which is true  back into our variable success after we call
  250. 24:29cortine do resume and then we can print that here  in the output which is going to be true so that's
  251. 24:36another interesting thing you can do with cortine  yield is return arguments in case we want to pass
  252. 24:43in any information from yielding to then do other  things with it we can pass in as many arguments as
  253. 24:49we want we can say true we can say I don't know  the number five we can say a string that's just
  254. 24:55false or anything like that and we can do do that  as long as we fulfill the number of variables that
  255. 25:03tie into the amount of arguments that we pass  back so this is Success we can separate this
  256. 25:07with a comma by saying uh number and then this  is going to be result or just anything it can
  257. 25:14be whatever you want it to be and we can print  all of these out here in the output too with
  258. 25:19number and result okay there was a little mistake  here so when we return a yield it's automatically
  259. 25:25going to it's going to implicit return true so  what this would look like is it would actually
  260. 25:31return true true five false because the first  true is already passed back into this variable
  261. 25:38success so what it's actually going to look like  is it's going to be success and then it's going
  262. 25:43to be whatever results we have after that so it's  pretty much going to look like this we have our
  263. 25:47first result here which is true so we're just  going to call this results one and then we have
  264. 25:51our second result which is five we'll call this  result two and then finally results three which is
  265. 25:57false we can re we can print all these variables  out so result 1 comma result 2 comma result 3 so
  266. 26:05this is pretty much how it's going to look like  success is going to implicitly be returned to
  267. 26:11let us know that yielding was successful and then  we have result one which is tied to True Result
  268. 26:17two which is tied to five and then result three  which is tied to false so if we run the script
  269. 26:21now we should see all of our arguments being uh  printed inside of the consoles so we first have
  270. 26:27our one and then we have result one result two  and then result three which is true five false
  271. 26:34so it basically works as intended all right now  you might be sort of confused when we would use
  272. 26:39cortines inside of our Roblox games when we're  creating them because so far what we just did
  273. 26:44with our scripts just looks like a whole bunch  of Concepts just scrambled on together inside
  274. 26:49of our script without really doing much so let me  just lay down a couple of ideas of when you would
  275. 26:55potentially use cortines during your development  there's a few ideas I can think of off the top of
  276. 27:00my head one of them that sticks out to me is  a countdown timer so if you're playing round
  277. 27:05based game for instance you want the timer to  constantly be de to constantly be decrementing
  278. 27:12the timer every 1 second while other things are  happening in the round other than just waiting
  279. 27:17for the timer to be over like giving the player  weapons making changes to the map having events
  280. 27:22happen there's a whole bunch of things you can  do with a round based system and you want the
  281. 27:28timer and you want the countdown timer to be  one of them not the only thing happening so
  282. 27:33that's one example another one could be a wave  based enemy spawning system where like let's
  283. 27:38say you're playing a tower defense game you want  the waves to be split up into different chunks
  284. 27:43throughout the entire match and you don't want  all the zombies to be spawning in continuously
  285. 27:49at once during the whole game you want to break  them down into waves and within these waves you
  286. 27:55want to wait for all of the enemies to be killed  first before moving on to the next wave that's
  287. 28:01another example I can think of using cortines  but the imagination is definitely left to the
  288. 28:06Creator's mind so there's definitely use cases  for them you just have to know what feels right to
  289. 28:11you and I believe in your ability to do that you  might also ask what's the difference between task.
  290. 28:16spawn and using coroutines so if you remember  from my task functions video I basically stated
  291. 28:21that task. spawn is used to simultaneously run  a task alongside a different task but here's the
  292. 28:28thing about cortines one of the things that makes  cortines special is its use of being able to yield
  293. 28:36or pause the current thread and do something else  in the meantime and resume the thread whenever we
  294. 28:43need to continue what we left off with that thread  in a way where we have more control over what gets
  295. 28:50executed before what so let's go back to our  script and let's delete everything that's over
  296. 28:54here if we wanted to let's say make a coroutine  that does stuff but we also want our first task
  297. 29:03to do a little bit of stuff before moving on to  the second one and then back to the first one we
  298. 29:08can do that with cortines but we cannot do that  with task. spawn let me show you what this looks
  299. 29:13like with task. spawn first so we're going to  say task. spawn and then we're going to create
  300. 29:18a new function here let's write a while true do  statement here and let's just print some random
  301. 29:25stuff here with task. spawn task. weight 01 and  then down here we're going to write another while
  302. 29:32true do with print outside spawn and then task.  weight 0.01 let's see what this looks like if we
  303. 29:41hit run then we should constantly see task.  spawn and outside spawn running at the same
  304. 29:46time but we're not going to be able to control  doing specific things within one task and then
  305. 29:52doing specific things within another task and  then switching back to the first one in a in a
  306. 29:57more strict order we cannot do that with task.  spawn but we can do that with coroutines let me
  307. 30:03show you what this looks like with coroutines  let's delete our task. spawn and let's do this
  308. 30:07by saying local Loop 1 equals cortine do create  open parentheses close parenthesis function open
  309. 30:16parentheses close parentheses enter and down here  let's call the loop let's say quinee do resume
  310. 30:23Loop one and we're going to have some tasks going  on here as well let's say 4 I = 11 10 do print I
  311. 30:33so let's do so let's put another for Loop here  by saying 4 I = 1 10 do and let's print I and
  312. 30:42now down here let's also make a check let's say  if I equals 4 in this case then what we're going
  313. 30:49to do is pause our cortine by saying cortin  do yield open parentheses close parentheses
  314. 30:55and then down here let's resume our quartin Again  by just copying this and putting this down here
  315. 31:02now let's run the game and see what happens the  order in which I printed this stuff out is very
  316. 31:06interesting basically what happened here is that I  resumed I started execution of my cortine which I
  317. 31:14basically started at one and then it kept printing  the number and it's constantly checking whether uh
  318. 31:21the number reaches four so once it reached four  down here it basically stopped and then it went
  319. 31:26to our outside for Loop to print 10 numbers and  then it continued our cortine after it was over
  320. 31:36where it stopped at four and then it just kept on  repeatedly continuing until it reached the 10th
  321. 31:42number and that's where the cortine ended after we  resumed it ended at 10 so what I'm trying to get
  322. 31:48at here is you can't have this level of control  over what gets executed when you're using task.
  323. 31:55spawn over cortines I really hope this makes sense  in essence quines let you have more control over
  324. 32:03what gets executed when you're running multiple  tasks it it doesn't work the same way with task.
  325. 32:09spawn because it's basically like two different  threads running individually while with cortines
  326. 32:16you have two different tasks but they sort of run  interchangeably depending on how you handle these
  327. 32:23cortines so that's pretty much going to wrap up  cortines it is a pretty complicated concept so if
  328. 32:29there was if there was something you didn't really  understand about quarantines I would advise you to
  329. 32:33rewatch this video and also maybe try and watch  other YouTubers who also cover the same topic of
  330. 32:39quarantines maybe their explanation will help  you into fully understanding how cortines work
  331. 32:45and also just try to apply these inside of  your Roblox games with more practice and more
  332. 32:50familiarity and just pretty much understanding  how cortines and multi-thread processing and all
  333. 32:56of these other Concepts put together work then  I think you'll generally have an easier time
  334. 33:01implementing these in the future if you decide  to use them because cortines are basically an
  335. 33:07indispensable Concepts that are used quite a lot  for script multitasking and general stuff like
  336. 33:15that it's very tough but part of the journey  that's fun is just figuring the stuff out and
  337. 33:19then once you have figured the stuff out it feels  so rewarding to that you now know a complicated
  338. 33:25concept that you didn't previously know about  and that's just what's really fun about it so
  339. 33:29I hope your journey serves well for you and I  hope you found this video to be helpful join
  340. 33:33my Discord server the link is in the description  and I will see you in the next episode take care

About this transcript

This page contains the full transcript of Coroutines - Roblox Advanced Scripting #8 by BrawlDev, generated from the public captions YouTube serves with the video. The transcript has 6,298 words across 340 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.