How to check if an object exists in workspace without FindFirstChild?

I have a table where I store all objects instances:

TabObj = workspace:GetChildren()

will print, for example:

    [1] = Camera,
    [2] = Baseplate,
    [3] = Terrain,
    [4] = Part,
    [5] = Part,
    [6] = Part

… then I manually remove one of those 3 parts from the workspace.
Then TabObj will have an orphan item.

How can I loop through this table to check if some item is missing in the workspace?

PS: I think FindFirstChild will not work here, since it uses the instance name, but the table has the instance reference instead.

What’s your use case? Seems like you’ve run into a problem that shouldn’t be a problem and there might be a different or better way of getting a resolution to your actual problem. You can’t check for something’s existence in the workspace without FindFirstChild, necessarily.

As far as the table goes, well you could simply just use ipairs and use FindFirstChild with the value, but I’m not even sure what your table is meant to represent, either instances or names of instances. FindFirstChild would also conflict with the three parts named the same way; only one of them would need to exist for FindFirstChild to return true.

1 Like

The problem presented is only a reduction of the real problem; but basically this is it: I have a dictionary that stores the references of the existing objects.
I have a complex algorithm and due to some rare bug, sometimes the objects in the workspace do not match the respective objects in that dictionary, that is, the dictionary holds a reference to an instance that no longer exists in the workspace.
In this way, I want to do an internal checkup to check if any object in the table no longer exists. If this happens, I will remove its correspondent dictionary entry.
But, as the previous example shows, there are several parts with the same name. This way, FindFirstChild will not work, as this will search for an object by name and not by the direct reference that is in the table.
In summary, I want to know if there is a way to test whether an object exists in the workspace, using the instance reference and not its name.

IsDescendantOf. You can check if the Instance still exists in the workspace or even the DataModel.

for index, instance in pairs(instanceList) do
    if not instance:IsDescendantOf(game) then
        instanceList[index] = nil
    end
end

I assume this is roughly what you’re looking for.

2 Likes