User:JulienDethurens/Essays/Removing objects

From Legacy Roblox Wiki
Jump to navigationJump to search

In a recent update, two methods were introduced, and an older method, Remove, was deprecated. In this article, I am going to explain everything and remove all the confusion about these.

remove

The remove method was already deprecated when this update happened, because it uses camelCase, which was deprecated quite some time ago. The remove method should NEVER be used, there is just no reason at all to use it. This method does the same thing as the Remove method, but its name is different.

The remove method should never be used. There is no good reason to use it. There is no case which prefers the use of the remove method. Just don't use it.

Remove

The Remove method was deprecated in this update. What it did is set the Parent of every children of the object it is called on to nil, and call itself on all of them, resulting in all the descendants of the object being parented to nil.

The Remove method should never be used. There is no good reason to use it. There is no case which prefers the use of the Remove method. Just don't use it.

Destroy

The Destroy method was introduced in this update. What it does is set the Parent of the object it is called on to nil, lock the Parent property of the object and disconnect all the connections attached to events of the object.

Using the Destroy method instead of the Remove method makes you benefit from the fact it disconnects all the connections related to the object, connections which you don't need. What this means is much more elegant code on the C side and much better memory management.

Some players think Destroy skips the garbage collection and immediately deallocates the object, by removing it from the memory. However, that is not true, and these players are seriously mistaken. The Destroy method does manage the memory in a much better way than Remove, and could even be considered as a fix to a certain memory leak, but it does not directly deallocate the object.

ClearAllChildren

Because some persons incorrectly used the Remove method to clear an object from all its descendants, the admins also decided to make a method that does exactly that: ClearAllChildren. This method simply sets the Parent of all the descendants of an object to nil, by setting the Parent of all the children of the object to nil and then calling itself on all of them.

Parent Property

Some persons incorrectly used the Remove method to store an objet in nil temporarily. Not only is that an incorrect way to do it, but there was already a correct way to do it: setting its Parent property to nil.

If you want to store an object out of the game hierarchy temporarily, set its Parent property to nil.

Debris:AddItem

Some persons, after this update, just started recommending to use the AddItem method of the Debris service with 0 for the second argument. That is actually completely wrong. The AddItem method of the Debris service should be used whenever you know you won't need an object in a certain time, and you know that time, but you still need it for now. For example, if you don't need a certain GUI anymore, you should use the Destroy method to get rid of it. If, however, you are showing a message and you want it to disappear after 5 seconds, you should use the AddItem methods, in the following way:

game:GetService('Debris'):AddItem(message, 5)

The AddItem method of the Debris service should NEVER be used with a lifetime of 0.

Conclusion (AKA: tl;dr)

If you don't need an object anymore and want to get rid of it (most common case), use the Destroy method on it:

object:Destroy()

If you want to store an object temporarily out of the game hiearchy, set its Parent to nil:

object.Parent = nil

If you want to remove all the descendants of an object, use the ClearAllChildren method on it:

object:ClearAllChildren()

If you still need an object for now, but you know you won't need it in x seconds, use the AddItem method of the Debris service (where lifetime is the time, in seconds, that you still need to keep it for):

game:GetService('Debris'):AddItem(object, lifetime)
The Remove and the remove methods should never be used. There is no good reason to use them. There is no case which prefers the use of them over another. Just don't use them.