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
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.
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 (And if there’s a descendant added which works in the same way, Ima be so happy.