YouTube2Text

Collection Service - Roblox Advanced Scripting #10 — Transcript

by BrawlDev · 4,296 words · 225 segments · language en · Watch on YouTube

Full transcript

  1. 0:00I want to make an obstacle course but let's  imagine this for a minute if we had multiple kill
  2. 0:05bricks that we would be planning to use inside  of our Obby that we're trying to create we would
  3. 0:10probably want more than one of these kill bricks  since these kill bricks are basically run through
  4. 0:16a simple script if I open up workspace and open up  my part I made a script here where it's like if a
  5. 0:22player that has a humanoid touches it then their  health is set to zero that's pretty much how you
  6. 0:27make a kill brick so if we had one of these then  it pretty much works just as expected but what if
  7. 0:32we wanted to add more kill bricks to our obstacle  course which is what we're most likely going to do
  8. 0:37this is probably how you would do it okay so let's  take our part let's duplicate it put it over here
  9. 0:44diagonally over here and then let's take another  one just duplicate that and put it over here let's
  10. 0:48take all these three duplicate it and then just  drag it over this way okay so now it looks like we
  11. 0:54have a sustainable stage for our obstacle course  so this should pretty much work as expected but
  12. 1:01here is the issue now that we duplicated our part  that had our kilber script it also duplicated the
  13. 1:08same script for all these other parts that are  contained here with our one stage now why is this
  14. 1:15a problem it's a problem mainly because if we have  the same script being copied and pasted over and
  15. 1:20over again to all these different parts it's going  to be very disorganized and very confusing to just
  16. 1:27go back to each of these scripts and modify it if  I need to and it's also just bad for optimization
  17. 1:32in general because through our obstacle if we  touch any one of them it should basically fire
  18. 1:39that individual script for that individual part  but what I'm trying to get at here is that what if
  19. 1:44all of these were run in the same script and not  with six individual scripts I think that approach
  20. 1:51would definitely be a lot better than what we're  trying to do with individual scripts so that's
  21. 1:55what I'm going to be showing you how to do in this  episode using a feature called collection service
  22. 2:00so with that being said let's get straight into it  all right let's think about this for a minute so
  23. 2:03in my example with these kill bricks that we want  to be able to kill the player whenever they touch
  24. 2:09it we don't want the code for it like this code  right here to be inside of six different scripts
  25. 2:16when we're trying to make each of these parts  functional our goal is to try and take all of
  26. 2:20these parts and be executed by the same script  so it'll just run inside of one script and not
  27. 2:26six of them so how do we achieve this we achieve  this through through a feature called collection
  28. 2:31service and a good way to understand this is let's  say we have a collection of kill bricks we take
  29. 2:37all the parts that are inside of that collection  as if this was its own table and we run the code
  30. 2:43that we need to for each of these kill bricks  but but instead it could just be done through one
  31. 2:51script when we Loop through each of the objects  in the collection that's cool and all but now you
  32. 2:56might ask how do we make this collection of kill  bricks so the way we do that is by putting a tag
  33. 3:03for each of these kill bricks to represent the  collection that it's a part of so and so here's
  34. 3:10how we're going to do it we first need to figure  out how to create these tags and we can do that
  35. 3:14using roblox's built-in tag editor uh which can  be found up here inside of view we can hit this
  36. 3:20little button here that says tag editor and this  will pull up a window on the right side showing us
  37. 3:26an empty list of tags because we haven't created  any tags yet and so in order to create a tag we
  38. 3:32just need to press this button that says new tag  and it's going to highlight this text box in which
  39. 3:38we're just going to insert a tag name so in other  words this is pretty much the name we're going to
  40. 3:43give our kill bricks collection so we're just  going to call this kill bricks and we're going
  41. 3:49to hit enter so now we have our kill bricks tag  and now we need to apply this to all of our parts
  42. 3:55that we want to apply this that we want to add to  the Kil bricks collection so so let's just select
  43. 4:00all of our kill bricks and let's press this check  mark here that says apply this tag to all selected
  44. 4:06instances so let's press that and now all of these  parts should have the kill bricks tag attached to
  45. 4:13it and we can see if this works by just simply  selecting one of them and if this check mark is
  46. 4:20checked then that means it has the tag kill bricks  we can also check it down here in properties um
  47. 4:26inside of our tags category and we can see that  it's inside of the kill bricks category and we
  48. 4:32can see that it has the kill bricks tag so this  is how we use the tag Editor to how we add tags to
  49. 4:38our instances so now let's go and create a script  to take all of these items and make it functional
  50. 4:43we're going to do that by creating a script inside  of server script service so we're going to hit the
  51. 4:47plus sign create a new script let's just rename  this to collection service okay and so the first
  52. 4:54thing we're going to do is make reference to our  collection service service you know so we're going
  53. 5:01to say local collection service equals game colon  get service open parentheses close parentheses in
  54. 5:11quotations we're going to say collection service  so remember how I mentioned that when we tag all
  55. 5:19of these parts inside of a collection this  collection is essentially a table full of
  56. 5:25these instances where we can iterate through  all of these parts to then make the kill brick
  57. 5:32active and so the way we're going to retrieve  this table of items from the collection is by
  58. 5:38using a function called get tagged so this is  pretty much what it's going to look like so we're
  59. 5:43first going to create a variable to store our  table full of kill bricks so we're going to say
  60. 5:49local kill bricks equals collection service colon  get tagged and you have to remember remember that
  61. 5:59it is get tagged specifically I will go over the  other methods that can trip you up a little bit
  62. 6:04but just remember that get tagged is the method  of collection service that we're going to use to
  63. 6:10retrieve a table full of the items that we're  looking for inside of the collection so inside
  64. 6:17of these open and close parentheses we're going  to put in our collection name which over here we
  65. 6:23set it to be kill bricks so we're going to say  kill bricks and I do want to note that this is
  66. 6:29case sensitive so you have to make sure the name  is exactly what you made the Tag name to be this
  67. 6:35is pretty much our tag name that we created kill  bricks and we're basically telling collection
  68. 6:40service to give us a table of all of the kill  bricks that have the kill bricks tag that we gave
  69. 6:48it so now that we have this table full of kill  bricks let's Loop through all of these kill bricks
  70. 6:54so let's go down here and we're going to say for  I comma kill brick in pairs open parentheses Clos
  71. 7:04parentheses kill bricks and uh I want you to  note that I made a variable here called kill
  72. 7:10brick but this is referring to our kill bricks  table that we created so over here we're going
  73. 7:17to say do and now what we're pretty much going  to do is take our code that we had here with
  74. 7:23our touched event and basically just copy it over  to our for Loop so let's take our or script let's
  75. 7:30copy it and then paste it into here and now this  kill brick is basically applied to this touched
  76. 7:37event right here for each individual kill brick so  now it's pretty much looping through each of the
  77. 7:43kill bricks inside of our kill Brick's table and  adding a touched event to that kill brick to do
  78. 7:50exactly what we wanted to do so we don't have to  write six different scripts to put it all in each
  79. 7:57of these instances instead instead what we can do  is create a tag and apply that tag to all of these
  80. 8:05instances grab all of the instances from the tag  and then Loop through that table full of instances
  81. 8:12and do what we wanted to do and that's pretty  much it let's delete all of our original kill
  82. 8:16brick scripts that we had and let's try this out  in game to see if this works or not so let's go
  83. 8:22into the game hit test and hit play all right so  let's try out our Obby uh I'm able to get through
  84. 8:27it pretty normally but let's see what happens if  we step on one of them um so it looks like that
  85. 8:34this one worked when I stepped on it now let's  try stepping on this one this one seemed like it
  86. 8:38also worked too so our collection service script  should be running just as planned so we were able
  87. 8:45to do all of this inside of one script after  looping through all of the items inside of our
  88. 8:51collection to be able to do what we wanted it to  do rather than have six different scripts for each
  89. 8:57individual object so what I just showed you pretty  much demonstrates the whole concept of collection
  90. 9:02service but let me show you some other things you  can do with collection service so there's three
  91. 9:07commonly methods that trip people up when they're  working with collection service that being get
  92. 9:12tagged get tags and get all tags and I'm going  to be explaining the difference among all three
  93. 9:18of these so get tagged is basically a method in  collection service that retrieves a table full
  94. 9:27of all of the instances that have the tag that  we're trying to find and return so that we can
  95. 9:34set this up into a variable and then we can Loop  through all of the instances that do have this tag
  96. 9:40that we were looking for the next method I want to  introduce to you is called get tags which is not
  97. 9:45the same as get tagged so get tags is basically  saying we want to know all of the tags that are
  98. 9:53contained inside of one specific object that we  provide so you know how over here if we selected
  99. 10:00uh one of our parts and we go down here we can  see in the tags category that there's one tag in
  100. 10:06here called kill bricks we could even add another  tag in here by hitting this plus sign and we can
  101. 10:11even create a new tag right here just by saying  something just by giving it a new name like new
  102. 10:17tag or something so now this part has two tags  kill bricks and new tag so if we make reference
  103. 10:25to that specific part so let's go in here and  rame this part to part one and let's go into our
  104. 10:32script we're basically going to tell collection  service that we want to know all of the tags that
  105. 10:37are inside of part one and we want to return it  as a table so let's go in here and let's make a
  106. 10:42variable called part tags and we will set this  equal to collection service colon get tags open
  107. 10:52parentheses close parentheses and we're basically  going to throw in our specific part that we want
  108. 10:57to get the tags from which is part one inside of  workspace so we're going to say game. workspace
  109. 11:04dopart one and what we can pretty much do is just  Loop through these part tags so that we can print
  110. 11:12all of these out to let us know all the tags that  this specific part contains because what's pretty
  111. 11:17much going to print out is it's going to print out  kill bricks and new tag but let's see how this is
  112. 11:22written we're going to say for I tag in pairs  open parentheses closed parentheses part tags
  113. 11:30do and then we're going to print the tag so let's  go into the game and see our change all right so
  114. 11:35in the output we should see that kill bricks  and new tag is printed because those are all
  115. 11:42the tags that are set Within part one specifically  so that's the difference with get tagged and get
  116. 11:49tags but there's one more which is get all tags  which is a more Global more General method that
  117. 11:56you would use so in our get tags example this told  us to get the tags from a specific instance but
  118. 12:04what if we wanted to just get all the tags that  are available inside of the game then we would
  119. 12:09just simply say local all tags equals collection  service colon get all tags open parentheses closed
  120. 12:19parenthesis and there's no arguments that need to  be put inside of here and we can just do the same
  121. 12:24thing with our for Loop here we can just say 4 I  tag in pairs all tags do print tag so this will
  122. 12:37basically do the same thing as this but this is  going to get all the tags that exist inside of
  123. 12:42the game so inside of our tag editor if we had  like five or 10 different tags it's going to
  124. 12:49print all of those tags that exist inside of  the output so if we go into the game and hit
  125. 12:54play then we should see all the tags be printed  into the output okay so ignore these first two
  126. 12:59because this was for our first for Loop but for  our second for Loop it basically printed these
  127. 13:05three down here which was new tag kill bricks and  tag editor tag container don't worry about that
  128. 13:10one we did not create that one so that's basically  the difference between all three of them uh this
  129. 13:15one is not used very often but I would say get  tagged is the most useful one to know about and
  130. 13:21the one you're going to be using the most in your  scripts so with our tag editor we were able to
  131. 13:27create tags and add our parts to that tag before  joining into the game while having scripts run
  132. 13:34so I'm going to be showing you how to add these  tags and also remove them through scripting at
  133. 13:40any point throughout the game's runtime so let's  add in a new part so let's just go into workspace
  134. 13:46let's insert a new part and we're going to rename  this part to new part and let's make this special
  135. 13:53just by changing the size a little bit let's even  change the color of it as well okay so here's a
  136. 14:00new part and let's say we want to add this to our  kill bricks collection without manually doing it
  137. 14:06in the tag editor because all we can do is just  select this and click the check mark and it's
  138. 14:11already in the kill bricks collection but let's  say we want to do this through script we're going
  139. 14:15to use a method called add tag so let's comment  out these print statements that we have here what
  140. 14:21we're going to do is find this new part that's  inside of workspace and we're going to add it to
  141. 14:27our collection using a function called add tag  so this is what it's going to look like let's
  142. 14:33first find our new part by saying local new part  equals game. workpace colon wait for Child open
  143. 14:43parentheses close parth quotes new part and we're  wrapping this in a wait for child just in case the
  144. 14:49part doesn't load in fast enough during runtime  for the script so now what we'll do is we will
  145. 14:55use our add tag function so we're going to say  collection service colon add tag and this will
  146. 15:03take in two arguments so the first one is going  to be our instance that we want to add a tag to
  147. 15:09which is going to be our new part comma and our  second argument is going to be our collection or
  148. 15:17tag name which in this case it's Kil bricks so  in quotations we're going to say kill bricks so
  149. 15:26this add tag method it's going to take our new  part add our kill bricks tag to that part and
  150. 15:33of course we can remove the tag as well so let's  just make a task. we really quick task. wait for
  151. 15:38about 5 seconds and we're going to say collection  service colon remove tag open parentheses close
  152. 15:46parenthesis we're going to take our new part and  and we're going to remove the kill bricks uh tag
  153. 15:56name from that new part let's also Al add this  task. weight statement behind here so that we can
  154. 16:02really see the change when we load into the game  so let's add another task. weight statement right
  155. 16:07before we add the tag let's actually make this 7  seconds so that we can load into the game in time
  156. 16:13to see the change so let's go into the game hit  test and hit play and let me open up the workspace
  157. 16:19so that we can see the tags inside of our new  parts so let's scroll down here we can see that
  158. 16:23there's no tags and now we added the kill bricks  tag to our new part and after 5 Seconds it got
  159. 16:31rid of that tag from our new part so it basically  worked as expected this is useful for at any point
  160. 16:37during the game we want to add tags to to specific  parts to then do stuff to that part as part of a
  161. 16:45collection there's also another function where  we're allowed to check if a specific part has a
  162. 16:50specific tag that we're looking for so let's say  that after we add in a tag for our new part let's
  163. 16:55make a check to see if this new part still has the  kill bricks tag we're going to say if collection
  164. 17:03service colon has tag open parentheses close  parenthesis it's going to take in two arguments
  165. 17:11the first one's going to be the instance which is  going to be our new part comma and then our tag
  166. 17:16which is going to be kill bricks so if this new  part has the kill bricks tag then what we're going
  167. 17:23to do is print out a statement by saying new part  has the tag and let's copy this and put it down
  168. 17:33here after remove tag to see the difference so  what should happen is that this is going to print
  169. 17:38because new part does have the kill bricks tag  but this one is not going to go through because
  170. 17:45we removed the kill bricks tag from the new part  so as you can see we printed out new part has a
  171. 17:50tag but then in about 5 Seconds we're not going to  see the other print statement because we removed
  172. 17:56the kill bricks tag from the new parts so now  this if statement is not valid the new part does
  173. 18:03not have the new tag so this is not going to run  so collection service has provided us two events
  174. 18:08that we can use to check whether a tag has been  either added to a new part or if a tag has been
  175. 18:15removed from a part so we can have this event fire  whenever one of these two instances happen and we
  176. 18:23can do this using an event called get instance  added signal and get instance removed signal so
  177. 18:29this is pretty much what it's going to look like  let's go into our collection service script and
  178. 18:34let's drop two lines after our beginning for Loop  and this is what we're going to say we're going
  179. 18:39to say collection service colon get instance added  signal and then inside of these parentheses we're
  180. 18:49going to put in our tag name which is going to be  kill bricks and then this is going to be wrapped
  181. 18:56around in a function by saying colon connect  function open parentheses close parenthesis
  182. 19:03enter so this is pretty much what our header is  going to look like so we're going to use our get
  183. 19:09instance added signal which is basically saying  if there is a new instance that this tag has been
  184. 19:15applied to then we're going to execute whatever  is going to be down here inside of this function
  185. 19:20so let's just make a simple print statement by  saying print kill brick has been added to the
  186. 19:27collection and then we can pretty much do the  same thing with when we remove a kill brick tag
  187. 19:35from an instance as well so we're going to drop  two lines down here by saying collection service
  188. 19:41colon get instance removed signal and then over  here we're going to say kill bricks colon connect
  189. 19:51function open parentheses close parentheses enter  print kill brick has been removed moved from the
  190. 20:00collection and so there we go now we have two  instance signal events from collection service
  191. 20:07that keeps track of when a kill bricks tag has  been added to a part and when a kill bricks tag
  192. 20:14has been removed from a part so let's go into the  game and we should see our new change all right
  193. 20:20so as you can see the event has fired to print  kill brick has been added to the collection and
  194. 20:25in about 5 Seconds it should then say kill  brick has been removed from the collection
  195. 20:31which is exactly what we wanted the script to do  before I end off the video I want to show you an
  196. 20:35example of me using collection service alongside  other infrastructures for one of my games that
  197. 20:41I'm currently working on so over here we have  these obstacles of these spinning parts and if
  198. 20:47the player touches these parts then they basically  end up dying so in my game I have it set up to be
  199. 20:53compatible with objectoriented programming and  also streaming instances don't don't worry if
  200. 20:58you don't really know what that means right now  in the future we will go over more complicated
  201. 21:03topics like them but what I wanted to show you was  that these parts work perfectly fine with the way
  202. 21:08they're spinning and also when you touch them they  kill you so let's see what happens if I just run
  203. 21:13all the way back here to where we can barely see  them and it'll basically just disappear from our
  204. 21:20view when we walk far away enough so there you go  it's pretty much all the way back there and what
  205. 21:25should basically be happening is that the script  that's running them is basically dormant right
  206. 21:30now you can't even see or tell whether these are  running in the background or not and to tell you
  207. 21:35the truth they're actually not running they're  completely still the scripts that's running all
  208. 21:40of them are not even running right now because  they're out of view but once we go back into view
  209. 21:45where we can see them again with with streaming  instances we can see that it's running again back
  210. 21:50to where it originally was after the script had  stopped the functionality of these obstacles when
  211. 21:56we went out of view so collection service comes  a long way uh when you add these inside of your
  212. 22:02Roblox games there's a combination of things that  you can put together to create really powerful
  213. 22:07stuff like with the use of instant streaming and  object-oriented programming alongside collection
  214. 22:14service you can have really powerful Frameworks  that enhances optimization for your Roblox game
  215. 22:19and that's pretty much what collection service  is all about improving and optimizing your Roblox
  216. 22:25game not only for the players but also for the for  the devs as well so that's kind of the whole idea
  217. 22:30here and you kind of have to experiment with  this stuff to really get collection service
  218. 22:35and all these other Advanced scripting Concepts  under your belt so that's just one last thing I
  219. 22:40wanted to show you so to pretty much summarize  collection service is a feature made by Roblox
  220. 22:45that heavily optimizes the way we organize our  scripts when we're trying to pretty much make a
  221. 22:52whole bunch of instances operate and function  the same way without having to to write the
  222. 22:58same script every single time but instead do it  all inside of one script that's pretty much the
  223. 23:04basic idea of collection service and I hope you'll  be able to implement these inside of your Roblox
  224. 23:09games for the future to come if you found this  video to be helpful be sure to subscribe and join
  225. 23:13my 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 Collection Service - Roblox Advanced Scripting #10 by BrawlDev, generated from the public captions YouTube serves with the video. The transcript has 4,296 words across 225 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.