This seems a bit strange. I've got a hard logic problem based on the concepts of encapsulation here: Basically when I was trying to make a universal "discard" method for my tables, I ran into the problem where I could not remove the references other objects had to an object being discarded. Thus, I could only copy around an object, but never remove it without access to the references needed to be removed.
Let me make that more simple. Roblox has a "Destroy()" Method for all parts in the game, which => Sets the Parent property to nil, locks the Parent property, disconnects all connections and calls Destroy() on all children.
So basically, when you call "Destroy" on any given instance, the object now will be nil, and any references to it will also be nil. If you set x = Instance.new("Part") and then x:Destroy(), when you try to print(x), you will get nil
In my case, I want to design an object which has a "discard" function. Like Destroy, when the discard method is fired, other references to the part will become nil, but the part itself will move to a "discarded" table to be kept for later.
Here's what I have so far
discard = {}
Part = {}
toolbox = {
hammerTool = Part,
sillyTool = nil
}
function Part.discard()
table.insert(discard, Part)
--How do I make it remove the reference toolbox.hammerTool holds?
end
So when I try to fire discard on the Part object, it will move a reference of the table to the discard table, but not remove it from it's original table:
--main--
toolbox.hammerTool.discard();
print("--toolbox--");
for _, v in pairs(toolbox) do
print(v)
end
print("--discard--");
for _, v in pairs(discard) do
print(v)
end
--OUTPUT
----toolbox--
--table: 22738DB0
----discard--
--table: 22738DB0
So, does anyone know a nifty way I could make it so that the discard method would remove the reference of toolbox.hammerToolwithout passing the reference as an argument?

1 comment
Holding a reference to an object you call "Destroy" on (creating a variable for it), will not become nil when destroyed. In your first example, "x" would still represent the object. Just something I thought I should throw in there.