Is there a way I can detect the object is destroyed?
Experiment script:
local mytable = {} local part = workspace.Part table.insert(mytable,part) print(#mytable) --Prints 1 '_' part:Destroy() print(#mytable) --Prints 1? 0_0 print(mytable[1]) --Prints Part? O_O
You're maintaining a reference to the part, which will prevent it from being garbage collected, and mytable
hasn't been garbage collected when you run your prints.
You could remove it from the table yourself, or use a weak table with the values being weak (__mode = "v"
):
http://wiki.roblox.com/index.php/Weak_tables