Delete Instances (Destroy(), Remove(), Debris) - Roblox Advanced Scripting #2 — Transcript
Full transcript
- 0:00hey thanks for stopping by in this episode of my Roblox Advanced scripting tutorial series I'm
- 0:04going to be discussing about delete instances what they are how they are used and why you should use
- 0:11them delete instances is a term that I kind of made up because it reflects on ways that Roblox
- 0:16has provided us tools to handle unnecessary or unneeded objects or instances that we just have
- 0:22lying around inside of our Roblox games that we create and in this guide I'm going to show you
- 0:28different ways of how Roblox has provided us these tools to help us with with those situations but in
- 0:34order to understand what I'm talking about let me show you an example all right let's
- 0:38think about this for a minute so I just quickly created something that I think will be a great
- 0:43example for what I'm about to show you in this episode so right in front of me I have a coin
- 0:49spawning pad that randomly spawns coins onto this pad and then after a certain period of time which
- 0:55is about 5 Seconds they end up disappearing uh the reason I have it the reason I hav't do this
- 1:00is because my initial intention was that when a player steps on one of these coins then they will
- 1:07add a point to their bleeder stat but I did not create that yet um and so what's really important
- 1:13to note here is that these coins when they're not being picked up they despawn on their own
- 1:19after 5 Seconds it doesn't have to rely on me to pick them up before they actually despawn because
- 1:24if I just come over here and pick up one of these coins they uh disappear uh like in intended uh we
- 1:31don't have to deal with them anymore once they're collected we just wait for the next one to spawn
- 1:34in and so Roblox has provided us three methods to deal with with deleting instances and I'm going
- 1:40to be showing you how to do all three of them in this episode all right like I mentioned with
- 1:44my example we don't want to keep these parts lying around inside of the game that we don't
- 1:48need anymore otherwise it'll slow down Roblox and we have to have a healthy amount of garbage
- 1:55collection when it comes to handling what parts we don't need and and what parts we should keep
- 2:01in the world so there's something I want you to understand in our Explorer we have all of these
- 2:07instances and parts they all have a parent that is basically like the folder that the object or
- 2:15part or whatever it is is contained inside so if we look at our base plate our base Plate's parent
- 2:23our parent property here is workspace which is basically the folder of base plate base plate
- 2:29is a child also known as like a file inside of a folder inside of workspace that also applies to
- 2:36everything else that's here inside the workspace you have the camera terrain spawn location and
- 2:41let's say I were to insert a new part so if I go to model up here and then click on part
- 2:46I inserted a new part into the workspace but if I go over here to the right and click on part in in
- 2:53the Explorer we can see that the parent is also inside of the workspace and so that's something
- 2:58I want you to keep aware of when I'm going to be discussing about remove and how this affects parts
- 3:03inside of the game so now that we have this part how do we want to remove it through script well
- 3:08the first thing we're going to do is obviously create a script and uh on the right side let's
- 3:13create a script that'll be parented to part so we will click the plus sign that's in part and
- 3:19then we will say script so now as we can see here with the script the parent property of the script
- 3:24is the part itself so we want to be able to locate that part and uh we're going to do this by first
- 3:30deleting this and then we'll make reference to that part by saying local part equals script.
- 3:37parent so we've made reference to the part that's basically located on the script's parent and I'll
- 3:44simply show you the first method but before we do that let's actually create a weight uh at the
- 3:48very beginning of the script so that we can see the change happening when we delete it so we're
- 3:53going to say task. weight and then inside of these parentheses we're going to put put a
- 3:59number indicating the number of seconds we're going to wait until we execute this script so
- 4:04we'll say six and then down here let's say part colon remove and then we will wrap these around
- 4:12in open and close parentheses so let's go into the game let's hit test and hit play so that we
- 4:18can actually see the change that happens and so let me open the Explorer really quick so we can
- 4:23really see the change so after about 6 seconds as you can see the part disappeared not only in the
- 4:28workspace but also in the Explorer so that prettyy much did what we intended it to do so what's the
- 4:34issue here there's a lot of intricate stuff that goes on in the back of roblox's servers and game
- 4:41management stuff like that that it's kind of a bit complicated but effectively what this did
- 4:47was that it took the parts parent over here if I go into the Explorer click on parts and then
- 4:53see its parent this effectively set the parent of the part to nil and as you know know nil is
- 5:01a nonone value it's a value that does not exist so now that we know that remove basically sets
- 5:08the parent of the of the part to null it's sort of just floating around in the game's content but now
- 5:17the game doesn't know what to do with it because there is no parent set to it so it didn't fully
- 5:22get rid of it so Roblox has provided a better alternative called destroy so if we go into our
- 5:28script and replace remove with destroy instead we can actually see that it does multiple things it
- 5:36not only sets the parent property to nail like remove does but it also locks the parent of the
- 5:42instance and it disconnects all connections and calls destroy on all children so it basically
- 5:49does what remove does but as an added layer of security it does all these other things in order
- 5:55to provide as much performance as the game can give you uh when you get rid of a part so all
- 6:03in all this is just a much better method to use uh over remove so I mentioned in my example in
- 6:08the beginning of this tutorial that I basically had coins that came into the game and then after
- 6:14a certain period of time I made them disappear that was actually that actually didn't have to
- 6:19do with putting in a weight statement before destroying it there's actually a much better
- 6:24way of dealing with that sort of stuff to know um after a certain amount of time to to just delete
- 6:31the instance whenever I need it to without using a weight statement and I'll show you two ways of
- 6:36doing this and why I recommend this third method rather than using a weight statement so this third
- 6:41method is called debris and this is basically a model that Roblox has provided where it handles
- 6:49garbage collection when you want to delete a part within a certain within a specific amount of time
- 6:56rather than having to delete the part through one single command so if we go back up here to
- 7:02our script we can see that we have our weight statement here in the very first line before
- 7:07actually destroying the part I actually would not advise doing this by using a weight statement and
- 7:12then destroying the part I will explain to you why in just a little bit so let's delete this first
- 7:19line up here with the weight statement and let's also just comment out this part really quick by
- 7:24putting in the two dashes in front of the command so let's hit enter down here and and let's type in
- 7:31game. debris because this is uh the framework that we're going to be using in order to handle garbage
- 7:38collection and we're going to say colon add item in the first argument we're going to throw in the
- 7:46part and then we're going to separate using a comma for a second argument and we're going to
- 7:52give a number so we used the number six for the the time we wanted to use for uh when the part was
- 7:59actually are going to be destroyed so this is how we're going to do it instead of using task. wait 6
- 8:04seconds and then destroying the parts we're simply doing this in one line by saying game. debris and
- 8:10then this is the method we're using add item we want to add this part to the the debris garbage
- 8:17collection um after 6 seconds that this line has been executed so if we go back into the game and
- 8:26then hit play We Should pretty much see the same exact result just in a different just using a
- 8:33different framework so in about 6 seconds the part should be removed if we look at Explorer then the
- 8:39part is definitely not there in the Explorer anymore so that effectively did the exact same
- 8:44thing as destroy but we gave it a certain but we gave it a certain time before it actually deleted
- 8:50itself now it's time to answer the question why should we use debris over using a weight statement
- 8:56and then destroying the part okay so going back to my coins example Le uh here's the script right
- 9:01here the only thing you really need to know about is the um delete instance part where I
- 9:07basically have a weight statement here that Waits 5 seconds and then it destroys the part let's this
- 9:13is not a good idea let's see what happens when we actually do decide to have a weight statement and
- 9:18then destroy it when you saw my original example every single second there was a new coin that was
- 9:24spawning in and then it was going to delete itself after 5 Seconds it wasn't dependent on any other
- 9:30coin that was inside of the spawn location before it deleted itself but now it seems like when I
- 9:35added a weight statement it seems to wait for the coin to disappear first before spawning in
- 9:41another one this is clearly not what we want and the reason I can explain why this is happening is
- 9:47because effectively when we spawn in a coin using calling this function here um in our while true do
- 9:55statement it creates a new coin it sets that new coins parent to workspace uh and then it's going
- 10:04to wait 5 seconds before destroying this new coin but what you don't realize is happening is that
- 10:11this function is being called and we've added in a weit statement here where we're literally
- 10:17telling the script to stop what it's doing for 5 seconds and then destroy the new coin before
- 10:23ending the function and then jumping back down to here to start the script all over again the
- 10:29reason this is ineffective is because we're actually telling the script to wait 6 seconds
- 10:35instead of one before spawning in another coin so 5 Seconds here for it to be destroyed and
- 10:42then another second here in order to spawn the coin again when in actuality we want we want
- 10:48the loop to go every second rather than every 6 seconds and so when we use debris it's actually
- 10:55not dependent on the previous coin before it it spawns in the next one so if I go back to
- 11:01the script and quickly comment these two points out and then uncomment the new one game. debris
- 11:08add item new coin 5 Seconds we will see that it's not dependent on the previous coin before
- 11:14spawning in the next one so instead of waiting 6 seconds so waiting 5 seconds for the first one to
- 11:20despawn and then wait another second to spawn in the next one it just simply Waits 1 second before
- 11:27spawning in the next one and then independently each coin will despawn after 5 Seconds that's
- 11:33why I think using ad item for game. debris is a better method of when you want to get rid of an
- 11:40item after a specific number of time so in summary remove basically just sets the parent of whatever
- 11:46instance you're trying to get rid of to nil and I'm just going to say do not use move it is not
- 11:53a very effective strategy to get rid of stuff but it it's still great to understand how this
- 11:58stuff works Works destroy gets rid of whatever instance you're trying to get rid of in the most
- 12:05optimal way possible and you should only use this if you want to immediately get rid of an object
- 12:13without having to wait for it and then debris should be used when you want to get rid of an
- 12:19instance after a set amount of time that you give it it's important to know when to use debris and
- 12:26when to use destroy I hope you found this episode to be helpful uh be sure to check out my other
- 12:31videos on my Advanced scripting tutorial series and I will see you in the next one take care
About this transcript
This page contains the full transcript of Delete Instances (Destroy(), Remove(), Debris) - Roblox Advanced Scripting #2 by BrawlDev, generated from the public captions YouTube serves with the video. The transcript has 2,332 words across 124 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.