Does destroying an object also remove it from the table?

I’m not on a PC right now, but I’m curious.

Will destroying an object remove it from the table it is in, or will it turn into nil inside the table?

Example:

local example = {object1, object2, object3};
example[2]:Destroy();

Will the above example turn into this?

local example = {object1, nil, object3};

Or this?

local example = {object1, object3};

It actually stays the same ({obj1, obj2, obj3}), you would have to set it to nil in the table to change

1 Like

I know this has been answered however the answer is actually a bit more complex and harder to explain, but in a tl;dr, it’ll become your second example.

Whenever you have a table, you have strong and weak references. All references by default are strong, meaning they cannot get garbage collected until specific requirements are met, and there are some primitive types, like strings, numbers, booleans, tables, etc which are strong references no matter the table strength iirc. To make a table weak, you have to give it a metatable and then use a metamethod, and if you want to learn more about that then go here.

Now, from what I can presume your “object2” is an Instance in a strong table. This means the reference to the object is always there however all though it’s deleted, the table has cached it’s values and as such still has access to them.

1 Like

Values with strong references to them will never be collected, you’d need to nil those strong references beforehand in order for the value to be collected by the garbage collector upon its direct next garbage collection cycle.

1 Like