TLDW logo

Delete Instances (Destroy(), Remove(), Debris) - Roblox Advanced Scripting #2

By BrawlDev

Summary

## Key takeaways - **Avoid 'remove()' for instance deletion**: The 'remove()' method sets an instance's parent to nil, leaving it unmanaged and potentially impacting game performance. It's recommended to use 'destroy()' or 'DebrisService' instead. [03:04], [05:01] - **'destroy()' offers robust instance removal**: 'destroy()' not only removes an instance by setting its parent to nil but also locks the parent, disconnects connections, and calls destroy on children, optimizing performance. [05:23], [05:36] - **Use 'DebrisService' for timed deletion**: The 'DebrisService' handles garbage collection for instances that need to be deleted after a specific time, offering a more efficient approach than using 'task.wait()' before 'destroy()'. [06:07], [06:41] - **'DebrisService' prevents spawn delays**: Using 'DebrisService' ensures that new instances spawn at the intended rate, as it doesn't create a delay by waiting for previous instances to be destroyed, unlike a manual 'task.wait()' before 'destroy()'. [08:50], [09:47] - **Choose 'destroy()' for immediate removal**: Opt for 'destroy()' when you need to immediately remove an object without any waiting period, ensuring it's handled optimally by Roblox's systems. [12:05] - **Use 'Debris' for delayed instance removal**: 'Debris' should be used when you want an instance to be removed after a set amount of time, making it ideal for managing temporary objects in your game. [12:13]

Topics Covered

  • Mastering instance parenting: The core of object management.
  • Why Roblox's 'remove' method is ineffective.
  • Why 'destroy' is superior to 'remove' for instances.
  • Avoid 'task.wait' for timed object despawns in loops.
  • Roblox Debris: The optimal solution for timed cleanup.

Full Transcript

hey thanks for stopping by in this episode of  my Roblox Advanced scripting tutorial series I'm  

going to be discussing about delete instances what  they are how they are used and why you should use  

them delete instances is a term that I kind of  made up because it reflects on ways that Roblox  

has provided us tools to handle unnecessary or  unneeded objects or instances that we just have  

lying around inside of our Roblox games that we  create and in this guide I'm going to show you  

different ways of how Roblox has provided us these  tools to help us with with those situations but in  

order to understand what I'm talking about  let me show you an example all right let's  

think about this for a minute so I just quickly  created something that I think will be a great  

example for what I'm about to show you in this  episode so right in front of me I have a coin  

spawning pad that randomly spawns coins onto this  pad and then after a certain period of time which  

is about 5 Seconds they end up disappearing uh  the reason I have it the reason I hav't do this  

is because my initial intention was that when a  player steps on one of these coins then they will  

add a point to their bleeder stat but I did not  create that yet um and so what's really important  

to note here is that these coins when they're  not being picked up they despawn on their own  

after 5 Seconds it doesn't have to rely on me to  pick them up before they actually despawn because  

if I just come over here and pick up one of these  coins they uh disappear uh like in intended uh we  

don't have to deal with them anymore once they're  collected we just wait for the next one to spawn  

in and so Roblox has provided us three methods to  deal with with deleting instances and I'm going  

to be showing you how to do all three of them  in this episode all right like I mentioned with  

my example we don't want to keep these parts  lying around inside of the game that we don't  

need anymore otherwise it'll slow down Roblox  and we have to have a healthy amount of garbage  

collection when it comes to handling what parts  we don't need and and what parts we should keep  

in the world so there's something I want you to  understand in our Explorer we have all of these  

instances and parts they all have a parent that  is basically like the folder that the object or  

part or whatever it is is contained inside so if  we look at our base plate our base Plate's parent  

our parent property here is workspace which is  basically the folder of base plate base plate  

is a child also known as like a file inside of a  folder inside of workspace that also applies to  

everything else that's here inside the workspace  you have the camera terrain spawn location and  

let's say I were to insert a new part so if  I go to model up here and then click on part  

I inserted a new part into the workspace but if I  go over here to the right and click on part in in  

the Explorer we can see that the parent is also  inside of the workspace and so that's something  

I want you to keep aware of when I'm going to be  discussing about remove and how this affects parts  

inside of the game so now that we have this part  how do we want to remove it through script well  

the first thing we're going to do is obviously  create a script and uh on the right side let's  

create a script that'll be parented to part so  we will click the plus sign that's in part and  

then we will say script so now as we can see here  with the script the parent property of the script  

is the part itself so we want to be able to locate  that part and uh we're going to do this by first  

deleting this and then we'll make reference to  that part by saying local part equals script.  

parent so we've made reference to the part that's  basically located on the script's parent and I'll  

simply show you the first method but before we  do that let's actually create a weight uh at the  

very beginning of the script so that we can see  the change happening when we delete it so we're  

going to say task. weight and then inside of  these parentheses we're going to put put a  

number indicating the number of seconds we're  going to wait until we execute this script so  

we'll say six and then down here let's say part  colon remove and then we will wrap these around  

in open and close parentheses so let's go into  the game let's hit test and hit play so that we  

can actually see the change that happens and so  let me open the Explorer really quick so we can  

really see the change so after about 6 seconds as  you can see the part disappeared not only in the  

workspace but also in the Explorer so that prettyy  much did what we intended it to do so what's the  

issue here there's a lot of intricate stuff that  goes on in the back of roblox's servers and game  

management stuff like that that it's kind of a  bit complicated but effectively what this did  

was that it took the parts parent over here if  I go into the Explorer click on parts and then  

see its parent this effectively set the parent  of the part to nil and as you know know nil is  

a nonone value it's a value that does not exist  so now that we know that remove basically sets  

the parent of the of the part to null it's sort of  just floating around in the game's content but now  

the game doesn't know what to do with it because  there is no parent set to it so it didn't fully  

get rid of it so Roblox has provided a better  alternative called destroy so if we go into our  

script and replace remove with destroy instead we  can actually see that it does multiple things it  

not only sets the parent property to nail like  remove does but it also locks the parent of the  

instance and it disconnects all connections and  calls destroy on all children so it basically  

does what remove does but as an added layer of  security it does all these other things in order  

to provide as much performance as the game can  give you uh when you get rid of a part so all  

in all this is just a much better method to use  uh over remove so I mentioned in my example in  

the beginning of this tutorial that I basically  had coins that came into the game and then after  

a certain period of time I made them disappear  that was actually that actually didn't have to  

do with putting in a weight statement before  destroying it there's actually a much better  

way of dealing with that sort of stuff to know um  after a certain amount of time to to just delete  

the instance whenever I need it to without using  a weight statement and I'll show you two ways of  

doing this and why I recommend this third method  rather than using a weight statement so this third  

method is called debris and this is basically a  model that Roblox has provided where it handles  

garbage collection when you want to delete a part  within a certain within a specific amount of time  

rather than having to delete the part through  one single command so if we go back up here to  

our script we can see that we have our weight  statement here in the very first line before  

actually destroying the part I actually would not  advise doing this by using a weight statement and  

then destroying the part I will explain to you why  in just a little bit so let's delete this first  

line up here with the weight statement and let's  also just comment out this part really quick by  

putting in the two dashes in front of the command  so let's hit enter down here and and let's type in  

game. debris because this is uh the framework that  we're going to be using in order to handle garbage  

collection and we're going to say colon add item  in the first argument we're going to throw in the  

part and then we're going to separate using a  comma for a second argument and we're going to  

give a number so we used the number six for the  the time we wanted to use for uh when the part was  

actually are going to be destroyed so this is how  we're going to do it instead of using task. wait 6  

seconds and then destroying the parts we're simply  doing this in one line by saying game. debris and  

then this is the method we're using add item we  want to add this part to the the debris garbage  

collection um after 6 seconds that this line has  been executed so if we go back into the game and  

then hit play We Should pretty much see the same  exact result just in a different just using a  

different framework so in about 6 seconds the part  should be removed if we look at Explorer then the  

part is definitely not there in the Explorer  anymore so that effectively did the exact same  

thing as destroy but we gave it a certain but we  gave it a certain time before it actually deleted  

itself now it's time to answer the question why  should we use debris over using a weight statement  

and then destroying the part okay so going back  to my coins example Le uh here's the script right  

here the only thing you really need to know  about is the um delete instance part where I  

basically have a weight statement here that Waits  5 seconds and then it destroys the part let's this  

is not a good idea let's see what happens when we  actually do decide to have a weight statement and  

then destroy it when you saw my original example  every single second there was a new coin that was  

spawning in and then it was going to delete itself  after 5 Seconds it wasn't dependent on any other  

coin that was inside of the spawn location before  it deleted itself but now it seems like when I  

added a weight statement it seems to wait for  the coin to disappear first before spawning in  

another one this is clearly not what we want and  the reason I can explain why this is happening is  

because effectively when we spawn in a coin using  calling this function here um in our while true do  

statement it creates a new coin it sets that new  coins parent to workspace uh and then it's going  

to wait 5 seconds before destroying this new coin  but what you don't realize is happening is that  

this function is being called and we've added  in a weit statement here where we're literally  

telling the script to stop what it's doing for  5 seconds and then destroy the new coin before  

ending the function and then jumping back down  to here to start the script all over again the  

reason this is ineffective is because we're  actually telling the script to wait 6 seconds  

instead of one before spawning in another coin  so 5 Seconds here for it to be destroyed and  

then another second here in order to spawn the  coin again when in actuality we want we want  

the loop to go every second rather than every 6  seconds and so when we use debris it's actually  

not dependent on the previous coin before it  it spawns in the next one so if I go back to  

the script and quickly comment these two points  out and then uncomment the new one game. debris  

add item new coin 5 Seconds we will see that  it's not dependent on the previous coin before  

spawning in the next one so instead of waiting 6  seconds so waiting 5 seconds for the first one to  

despawn and then wait another second to spawn in  the next one it just simply Waits 1 second before  

spawning in the next one and then independently  each coin will despawn after 5 Seconds that's  

why I think using ad item for game. debris is a  better method of when you want to get rid of an  

item after a specific number of time so in summary  remove basically just sets the parent of whatever  

instance you're trying to get rid of to nil and  I'm just going to say do not use move it is not  

a very effective strategy to get rid of stuff  but it it's still great to understand how this  

stuff works Works destroy gets rid of whatever  instance you're trying to get rid of in the most  

optimal way possible and you should only use this  if you want to immediately get rid of an object  

without having to wait for it and then debris  should be used when you want to get rid of an  

instance after a set amount of time that you give  it it's important to know when to use debris and  

when to use destroy I hope you found this episode  to be helpful uh be sure to check out my other  

videos on my Advanced scripting tutorial series  and I will see you in the next one take care

Loading...

Loading video analysis...