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.
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.
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
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())
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.
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?
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
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.