Is there a way I can detect the object is destroyed?
Experiment script:
1 | local mytable = { } |
2 | local part = workspace.Part |
3 | table.insert(mytable,part) |
4 | print (#mytable) --Prints 1 '_' |
5 | part:Destroy() |
6 | print (#mytable) --Prints 1? 0_0 |
7 | 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