How can I check if a part exists in the workspace using an invalid instance without getting an error?

I want to identify whether an instance exists in the clientside workspace using the instance itself without getting an error. This means I can’t use FindFirstChild as far as I’m aware.

Since I have to reference what I’m looking for with the instance, FindFirstChild doesn’t work and IsDesendantOf errors as it can’t find the instance to call the function in the first place

I’ve looked around but all the posts I’ve found use FirstChildOf as the solution.

The code works something like this:

objectArray = {}
for count = 1, 3 do
    local part = new.instance("Part")
    part.Parent = workspace
    list.insert("objectArray", part)
end

--Second part is deleted

if objectArray[2]:IsDescendantOf(workspace) then
    print(":)")
end

--Error message appears
1 Like

My guess is looping through the workspace’s children and then checking if the instance’s name is equal to what you wanted. That’s probably not the best option though but it works.

1 Like

The issue is that the names of the parts can be identical, it may work if they have some sort of unique identifier though.

1 Like

Attribute them then and check for the attribute

1 Like
if workspace:FindFirstChild("part") ~= nil then
	-- it's there
end 

This is how you avoid “not there” without an error … also you can’t name all the parts the same name.

1 Like

You can have duplicate part names.

1 Like

You can’t find duplicate part names.

1 Like

You're problem


You already set the table and it won’t change unless something changes it.

My script


local objectArray = {};

for i = 1,3 do
    local part = Instance.new("Part", workspace)
    table.insert(objectArray, part) -- Adds to the list
    part.Destroying:Connect(function() -- If object is being destroyed then
        local index = table.find(objectArray, part) -- Finds index in the table
        objectArray[index] = nil -- Sets object to nil
    end)
end

objectArray[2]:Destroy() -- Destroys the object

if objectArray[2] ~= nil then -- If it exist
    print(":)")
else -- If it doesn't exist
    print(":(")
end

Note: Alter the script to benefit your game/program technique

The issue with Destroying is that it only works when Instance:Destroy() is used. I’m more concerned about detecting parts which get voided, reset, etc.

I found an alternative way that works for most destroyers:

Using DescendantRemoving


local part = Instance.new("Part", workspace)

part.CanCollide = false



local objectArray = {};

for i = 1,3 do
	local part = Instance.new("Part", workspace)
	table.insert(objectArray, part) -- Adds to the list
	workspace.DescendantRemoving:Connect(function(obj)
		if obj == part then 
			local index = table.find(objectArray, part) -- Finds index in the table
			objectArray[index] = nil	
		end
	end)
end

objectArray[2]:Destroy() -- Destroys the object

if objectArray[2] ~= nil then -- If it exist
	print(":)")
else -- If it doesn't exist
	print(":(")
end

Thanks a lot, this works great, now I can delete the entire second array workaround I used, was a logistical nightmare :slight_smile: (And if there’s a descendant added which works in the same way, Ima be so happy.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.