Collection Service - Roblox Advanced Scripting #10 — Transcript
Full transcript
- 0:00I want to make an obstacle course but let's imagine this for a minute if we had multiple kill
- 0:05bricks that we would be planning to use inside of our Obby that we're trying to create we would
- 0:10probably want more than one of these kill bricks since these kill bricks are basically run through
- 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
- 0:22player that has a humanoid touches it then their health is set to zero that's pretty much how you
- 0:27make a kill brick so if we had one of these then it pretty much works just as expected but what if
- 0:32we wanted to add more kill bricks to our obstacle course which is what we're most likely going to do
- 0:37this is probably how you would do it okay so let's take our part let's duplicate it put it over here
- 0:44diagonally over here and then let's take another one just duplicate that and put it over here let's
- 0:48take all these three duplicate it and then just drag it over this way okay so now it looks like we
- 0:54have a sustainable stage for our obstacle course so this should pretty much work as expected but
- 1:01here is the issue now that we duplicated our part that had our kilber script it also duplicated the
- 1:08same script for all these other parts that are contained here with our one stage now why is this
- 1:15a problem it's a problem mainly because if we have the same script being copied and pasted over and
- 1:20over again to all these different parts it's going to be very disorganized and very confusing to just
- 1:27go back to each of these scripts and modify it if I need to and it's also just bad for optimization
- 1:32in general because through our obstacle if we touch any one of them it should basically fire
- 1:39that individual script for that individual part but what I'm trying to get at here is that what if
- 1:44all of these were run in the same script and not with six individual scripts I think that approach
- 1:51would definitely be a lot better than what we're trying to do with individual scripts so that's
- 1:55what I'm going to be showing you how to do in this episode using a feature called collection service
- 2:00so with that being said let's get straight into it all right let's think about this for a minute so
- 2:03in my example with these kill bricks that we want to be able to kill the player whenever they touch
- 2:09it we don't want the code for it like this code right here to be inside of six different scripts
- 2:16when we're trying to make each of these parts functional our goal is to try and take all of
- 2:20these parts and be executed by the same script so it'll just run inside of one script and not
- 2:26six of them so how do we achieve this we achieve this through through a feature called collection
- 2:31service and a good way to understand this is let's say we have a collection of kill bricks we take
- 2:37all the parts that are inside of that collection as if this was its own table and we run the code
- 2:43that we need to for each of these kill bricks but but instead it could just be done through one
- 2:51script when we Loop through each of the objects in the collection that's cool and all but now you
- 2:56might ask how do we make this collection of kill bricks so the way we do that is by putting a tag
- 3:03for each of these kill bricks to represent the collection that it's a part of so and so here's
- 3:10how we're going to do it we first need to figure out how to create these tags and we can do that
- 3:14using roblox's built-in tag editor uh which can be found up here inside of view we can hit this
- 3:20little button here that says tag editor and this will pull up a window on the right side showing us
- 3:26an empty list of tags because we haven't created any tags yet and so in order to create a tag we
- 3:32just need to press this button that says new tag and it's going to highlight this text box in which
- 3:38we're just going to insert a tag name so in other words this is pretty much the name we're going to
- 3:43give our kill bricks collection so we're just going to call this kill bricks and we're going
- 3:49to hit enter so now we have our kill bricks tag and now we need to apply this to all of our parts
- 3:55that we want to apply this that we want to add to the Kil bricks collection so so let's just select
- 4:00all of our kill bricks and let's press this check mark here that says apply this tag to all selected
- 4:06instances so let's press that and now all of these parts should have the kill bricks tag attached to
- 4:13it and we can see if this works by just simply selecting one of them and if this check mark is
- 4:20checked then that means it has the tag kill bricks we can also check it down here in properties um
- 4:26inside of our tags category and we can see that it's inside of the kill bricks category and we
- 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
- 4:38our instances so now let's go and create a script to take all of these items and make it functional
- 4:43we're going to do that by creating a script inside of server script service so we're going to hit the
- 4:47plus sign create a new script let's just rename this to collection service okay and so the first
- 4:54thing we're going to do is make reference to our collection service service you know so we're going
- 5:01to say local collection service equals game colon get service open parentheses close parentheses in
- 5:11quotations we're going to say collection service so remember how I mentioned that when we tag all
- 5:19of these parts inside of a collection this collection is essentially a table full of
- 5:25these instances where we can iterate through all of these parts to then make the kill brick
- 5:32active and so the way we're going to retrieve this table of items from the collection is by
- 5:38using a function called get tagged so this is pretty much what it's going to look like so we're
- 5:43first going to create a variable to store our table full of kill bricks so we're going to say
- 5:49local kill bricks equals collection service colon get tagged and you have to remember remember that
- 5:59it is get tagged specifically I will go over the other methods that can trip you up a little bit
- 6:04but just remember that get tagged is the method of collection service that we're going to use to
- 6:10retrieve a table full of the items that we're looking for inside of the collection so inside
- 6:17of these open and close parentheses we're going to put in our collection name which over here we
- 6:23set it to be kill bricks so we're going to say kill bricks and I do want to note that this is
- 6:29case sensitive so you have to make sure the name is exactly what you made the Tag name to be this
- 6:35is pretty much our tag name that we created kill bricks and we're basically telling collection
- 6:40service to give us a table of all of the kill bricks that have the kill bricks tag that we gave
- 6:48it so now that we have this table full of kill bricks let's Loop through all of these kill bricks
- 6:54so let's go down here and we're going to say for I comma kill brick in pairs open parentheses Clos
- 7:04parentheses kill bricks and uh I want you to note that I made a variable here called kill
- 7:10brick but this is referring to our kill bricks table that we created so over here we're going
- 7:17to say do and now what we're pretty much going to do is take our code that we had here with
- 7:23our touched event and basically just copy it over to our for Loop so let's take our or script let's
- 7:30copy it and then paste it into here and now this kill brick is basically applied to this touched
- 7:37event right here for each individual kill brick so now it's pretty much looping through each of the
- 7:43kill bricks inside of our kill Brick's table and adding a touched event to that kill brick to do
- 7:50exactly what we wanted to do so we don't have to write six different scripts to put it all in each
- 7:57of these instances instead instead what we can do is create a tag and apply that tag to all of these
- 8:05instances grab all of the instances from the tag and then Loop through that table full of instances
- 8:12and do what we wanted to do and that's pretty much it let's delete all of our original kill
- 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
- 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
- 8:27it pretty normally but let's see what happens if we step on one of them um so it looks like that
- 8:34this one worked when I stepped on it now let's try stepping on this one this one seemed like it
- 8:38also worked too so our collection service script should be running just as planned so we were able
- 8:45to do all of this inside of one script after looping through all of the items inside of our
- 8:51collection to be able to do what we wanted it to do rather than have six different scripts for each
- 8:57individual object so what I just showed you pretty much demonstrates the whole concept of collection
- 9:02service but let me show you some other things you can do with collection service so there's three
- 9:07commonly methods that trip people up when they're working with collection service that being get
- 9:12tagged get tags and get all tags and I'm going to be explaining the difference among all three
- 9:18of these so get tagged is basically a method in collection service that retrieves a table full
- 9:27of all of the instances that have the tag that we're trying to find and return so that we can
- 9:34set this up into a variable and then we can Loop through all of the instances that do have this tag
- 9:40that we were looking for the next method I want to introduce to you is called get tags which is not
- 9:45the same as get tagged so get tags is basically saying we want to know all of the tags that are
- 9:53contained inside of one specific object that we provide so you know how over here if we selected
- 10:00uh one of our parts and we go down here we can see in the tags category that there's one tag in
- 10:06here called kill bricks we could even add another tag in here by hitting this plus sign and we can
- 10:11even create a new tag right here just by saying something just by giving it a new name like new
- 10:17tag or something so now this part has two tags kill bricks and new tag so if we make reference
- 10:25to that specific part so let's go in here and rame this part to part one and let's go into our
- 10:32script we're basically going to tell collection service that we want to know all of the tags that
- 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
- 10:42variable called part tags and we will set this equal to collection service colon get tags open
- 10:52parentheses close parentheses and we're basically going to throw in our specific part that we want
- 10:57to get the tags from which is part one inside of workspace so we're going to say game. workspace
- 11:04dopart one and what we can pretty much do is just Loop through these part tags so that we can print
- 11:12all of these out to let us know all the tags that this specific part contains because what's pretty
- 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
- 11:22written we're going to say for I tag in pairs open parentheses closed parentheses part tags
- 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
- 11:35in the output we should see that kill bricks and new tag is printed because those are all
- 11:42the tags that are set Within part one specifically so that's the difference with get tagged and get
- 11:49tags but there's one more which is get all tags which is a more Global more General method that
- 11:56you would use so in our get tags example this told us to get the tags from a specific instance but
- 12:04what if we wanted to just get all the tags that are available inside of the game then we would
- 12:09just simply say local all tags equals collection service colon get all tags open parentheses closed
- 12:19parenthesis and there's no arguments that need to be put inside of here and we can just do the same
- 12:24thing with our for Loop here we can just say 4 I tag in pairs all tags do print tag so this will
- 12:37basically do the same thing as this but this is going to get all the tags that exist inside of
- 12:42the game so inside of our tag editor if we had like five or 10 different tags it's going to
- 12:49print all of those tags that exist inside of the output so if we go into the game and hit
- 12:54play then we should see all the tags be printed into the output okay so ignore these first two
- 12:59because this was for our first for Loop but for our second for Loop it basically printed these
- 13:05three down here which was new tag kill bricks and tag editor tag container don't worry about that
- 13:10one we did not create that one so that's basically the difference between all three of them uh this
- 13:15one is not used very often but I would say get tagged is the most useful one to know about and
- 13:21the one you're going to be using the most in your scripts so with our tag editor we were able to
- 13:27create tags and add our parts to that tag before joining into the game while having scripts run
- 13:34so I'm going to be showing you how to add these tags and also remove them through scripting at
- 13:40any point throughout the game's runtime so let's add in a new part so let's just go into workspace
- 13:46let's insert a new part and we're going to rename this part to new part and let's make this special
- 13:53just by changing the size a little bit let's even change the color of it as well okay so here's a
- 14:00new part and let's say we want to add this to our kill bricks collection without manually doing it
- 14:06in the tag editor because all we can do is just select this and click the check mark and it's
- 14:11already in the kill bricks collection but let's say we want to do this through script we're going
- 14:15to use a method called add tag so let's comment out these print statements that we have here what
- 14:21we're going to do is find this new part that's inside of workspace and we're going to add it to
- 14:27our collection using a function called add tag so this is what it's going to look like let's
- 14:33first find our new part by saying local new part equals game. workpace colon wait for Child open
- 14:43parentheses close parth quotes new part and we're wrapping this in a wait for child just in case the
- 14:49part doesn't load in fast enough during runtime for the script so now what we'll do is we will
- 14:55use our add tag function so we're going to say collection service colon add tag and this will
- 15:03take in two arguments so the first one is going to be our instance that we want to add a tag to
- 15:09which is going to be our new part comma and our second argument is going to be our collection or
- 15:17tag name which in this case it's Kil bricks so in quotations we're going to say kill bricks so
- 15:26this add tag method it's going to take our new part add our kill bricks tag to that part and
- 15:33of course we can remove the tag as well so let's just make a task. we really quick task. wait for
- 15:38about 5 seconds and we're going to say collection service colon remove tag open parentheses close
- 15:46parenthesis we're going to take our new part and and we're going to remove the kill bricks uh tag
- 15:56name from that new part let's also Al add this task. weight statement behind here so that we can
- 16:02really see the change when we load into the game so let's add another task. weight statement right
- 16:07before we add the tag let's actually make this 7 seconds so that we can load into the game in time
- 16:13to see the change so let's go into the game hit test and hit play and let me open up the workspace
- 16:19so that we can see the tags inside of our new parts so let's scroll down here we can see that
- 16:23there's no tags and now we added the kill bricks tag to our new part and after 5 Seconds it got
- 16:31rid of that tag from our new part so it basically worked as expected this is useful for at any point
- 16:37during the game we want to add tags to to specific parts to then do stuff to that part as part of a
- 16:45collection there's also another function where we're allowed to check if a specific part has a
- 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
- 16:55make a check to see if this new part still has the kill bricks tag we're going to say if collection
- 17:03service colon has tag open parentheses close parenthesis it's going to take in two arguments
- 17:11the first one's going to be the instance which is going to be our new part comma and then our tag
- 17:16which is going to be kill bricks so if this new part has the kill bricks tag then what we're going
- 17:23to do is print out a statement by saying new part has the tag and let's copy this and put it down
- 17:33here after remove tag to see the difference so what should happen is that this is going to print
- 17:38because new part does have the kill bricks tag but this one is not going to go through because
- 17:45we removed the kill bricks tag from the new part so as you can see we printed out new part has a
- 17:50tag but then in about 5 Seconds we're not going to see the other print statement because we removed
- 17:56the kill bricks tag from the new parts so now this if statement is not valid the new part does
- 18:03not have the new tag so this is not going to run so collection service has provided us two events
- 18:08that we can use to check whether a tag has been either added to a new part or if a tag has been
- 18:15removed from a part so we can have this event fire whenever one of these two instances happen and we
- 18:23can do this using an event called get instance added signal and get instance removed signal so
- 18:29this is pretty much what it's going to look like let's go into our collection service script and
- 18:34let's drop two lines after our beginning for Loop and this is what we're going to say we're going
- 18:39to say collection service colon get instance added signal and then inside of these parentheses we're
- 18:49going to put in our tag name which is going to be kill bricks and then this is going to be wrapped
- 18:56around in a function by saying colon connect function open parentheses close parenthesis
- 19:03enter so this is pretty much what our header is going to look like so we're going to use our get
- 19:09instance added signal which is basically saying if there is a new instance that this tag has been
- 19:15applied to then we're going to execute whatever is going to be down here inside of this function
- 19:20so let's just make a simple print statement by saying print kill brick has been added to the
- 19:27collection and then we can pretty much do the same thing with when we remove a kill brick tag
- 19:35from an instance as well so we're going to drop two lines down here by saying collection service
- 19:41colon get instance removed signal and then over here we're going to say kill bricks colon connect
- 19:51function open parentheses close parentheses enter print kill brick has been removed moved from the
- 20:00collection and so there we go now we have two instance signal events from collection service
- 20:07that keeps track of when a kill bricks tag has been added to a part and when a kill bricks tag
- 20:14has been removed from a part so let's go into the game and we should see our new change all right
- 20:20so as you can see the event has fired to print kill brick has been added to the collection and
- 20:25in about 5 Seconds it should then say kill brick has been removed from the collection
- 20:31which is exactly what we wanted the script to do before I end off the video I want to show you an
- 20:35example of me using collection service alongside other infrastructures for one of my games that
- 20:41I'm currently working on so over here we have these obstacles of these spinning parts and if
- 20:47the player touches these parts then they basically end up dying so in my game I have it set up to be
- 20:53compatible with objectoriented programming and also streaming instances don't don't worry if
- 20:58you don't really know what that means right now in the future we will go over more complicated
- 21:03topics like them but what I wanted to show you was that these parts work perfectly fine with the way
- 21:08they're spinning and also when you touch them they kill you so let's see what happens if I just run
- 21:13all the way back here to where we can barely see them and it'll basically just disappear from our
- 21:20view when we walk far away enough so there you go it's pretty much all the way back there and what
- 21:25should basically be happening is that the script that's running them is basically dormant right
- 21:30now you can't even see or tell whether these are running in the background or not and to tell you
- 21:35the truth they're actually not running they're completely still the scripts that's running all
- 21:40of them are not even running right now because they're out of view but once we go back into view
- 21:45where we can see them again with with streaming instances we can see that it's running again back
- 21:50to where it originally was after the script had stopped the functionality of these obstacles when
- 21:56we went out of view so collection service comes a long way uh when you add these inside of your
- 22:02Roblox games there's a combination of things that you can put together to create really powerful
- 22:07stuff like with the use of instant streaming and object-oriented programming alongside collection
- 22:14service you can have really powerful Frameworks that enhances optimization for your Roblox game
- 22:19and that's pretty much what collection service is all about improving and optimizing your Roblox
- 22:25game not only for the players but also for the for the devs as well so that's kind of the whole idea
- 22:30here and you kind of have to experiment with this stuff to really get collection service
- 22:35and all these other Advanced scripting Concepts under your belt so that's just one last thing I
- 22:40wanted to show you so to pretty much summarize collection service is a feature made by Roblox
- 22:45that heavily optimizes the way we organize our scripts when we're trying to pretty much make a
- 22:52whole bunch of instances operate and function the same way without having to to write the
- 22:58same script every single time but instead do it all inside of one script that's pretty much the
- 23:04basic idea of collection service and I hope you'll be able to implement these inside of your Roblox
- 23:09games for the future to come if you found this video to be helpful be sure to subscribe and join
- 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.