How to find an object by it's instance reference / id?

I’m a little confused.
In my game, I save the references of the created object instances in a table.
In some cases, I destroy the object.
Then I have to scan this table and remove the object that was destroyed.

local tab = {}
local obj1 = Instance.new("Part", workspace)
local obj2 = Instance.new("Part", workspace)
tab[obj1] = true
tab[obj2] = true
obj1:Destroy()
print(tab)

… will print:

 ▼  {
    [Instance(1688E4F81F8)] = true,
    [Instance(1688E4F8228)] = true
  }

Now, how to test if Instance(1688E4F8228) still exists on the workspace?

One simple test would be checking if its parent is nil for example:

local destroyedInstance = -- Instance
if destroyedInstance.Parent == nil then
  -- do stuff
end
2 Likes
local destroyedObj = obj1
If not game.Workspace.destroyedObj then
 Print(“object was destroyed”)
End

just a heads up

you capitalized globals
and you used the wrong double quotes

EDIT:
fixed my reply

Yeah IPads aren’t really great for writing scripts.

1 Like

generate a guid for each part and use collectionservice to add the guid to it as a tag and use getinstanceremovedsignal to remove obj from table when it is destroyed

This won’t work (even fixing your typos):

destroyedObj is not a valid member of Workspace “Workspace”