Detect if Child w/ Certain Name Doesn't Exist

  1. What do you want to achieve?
    I am trying to make an if statement to detect if a child with a certain name doesn’t exist.

  2. What is the issue?
    I don’t know how to detect if a child with a certain name exists, much less if it doesn’t exist.

  3. What solutions have you tried so far?
    I have tried the following, each of which ceased to function:

if not workspace:WaitForChild("MyPart") then

if not workspace:FindFirstChild("MyPart") then

if not workspace:FindFirstChild("MyPart") == 0 then

if not workspace:FindFirstChild("MyPart") == false then

Thanks in advance!

Why are you doing if not workspace? If the part is in workspace then type if workspace. Like it doesn’t make sense.

try using pcall

local succ,err=pcall(function()
   workspace:FindFirstChild("MyPart")
end)
if succ then
  --part exists
else
  --part doesnt exist
end

i guess this work try it

The second code should work. If you want to check recursively, then you can do if not workspace:FindFirstChild("Name", true) then, and that should work as well. Alternatively, you could do if workspace:FindFirstChild("Name") == nil then. (You could add a true as well for recursive use)

EDIT: If you want to make it a bit more complicated, then you could make the following function:

function HasChild(Name, Model)
    for _, Child in pairs(Model:GetChildren()) do
        if Child.Name == Name then
            return true
        end
    end
    return false
end

(If you are searching recursively, use GetDescendants() instead of GetChildren())

Hope this helped.

2 Likes

I see no use for a pcall. The function :FindFirstChild() either returns the child or returns nil. Although a pcall works, it is much easier to just check if the child is nil or not.

1 Like

but doesnt it give an index nil error?

Not as far as I am aware. I have used it that way for many of my projects and none had errors of such.

Try this:

   for i,v in pairs(workspace:GetChildren() do
    if v.Name == "MyPart" then
    -- do something
    end
    end

I am doing if not because I want to see if a part isn’t there, not if it is there.

But you’re directly referencing the part, so you know its there. If you wanted a more secure check loop through to workspace and check to see if the name of the part is nil. if not workspace is just bad practice anyways, idk were you learned that from. You could even do if workspace:findfirstchild(‘name’) == nil then < this makes way more sense than if not workspace. Like idk what this even is. You also had if not workspace:FindFirstChild(“MyPart”) == 0 then < how are you finding a value of 0 out of a part?

if workspace:FindFirstChild("MyPart") == nil then
    --ez, If we try to find part and there's nothing, it returns NIL. So here ya go.
end

He was fine to use not in this context.
The solution here should be
if not workspace:FindFirstChild("name") then
or
if workspace: FindFirstChild ("name") == nil then
They function the same

Thanks for responding, this was the answer!

1 Like

If that was the case his problem would of been solved, as you see in his original example he already tried if not workspace:findfirstchild(‘name’) then < it didnt work. So clearly its not fine to use in this context.