Check if part exist

Hello Scripters,

I have a simple question to ask. I want to figure out if the part or instance is existing in the workspace if not to do something else

if Instance then
 print('Is there')
else
 print('Not there')
end
if(part:IsDescendantOf(workspace)) then
...
end

Part of wouldn’t work for me because ‘part’ doesn’t exist, how would i go about that?

‘part’ is the instance you want to check. I added it not as a pre-defined value but instead a placeholder so that you could put your actual part there.

Thank you for checking, but I implemented :IsDescendantOf() into my script but I want to check if a instance has existed yet or not

1 Like

you could use pcall function (What it does it wont error if the Part doesn’t exist)

pcall(function() 

end)

Or FindFirstChild (Recommended)

if not game.Workspace:FindFirstChild("YourPart") then

end
1 Like

I think what the op was trying to refer to was, they can’t use “IsDescendantOf” because the variable isn’t defined?

Maybe they are looking for:

Workspace:FindFirstChild(Name)

I see. In that case you can use is ancestor of:

workspace:IsAncestorOf(part)

And if you really don’t have the variable and just want to check if it is there you can use:

workspace:FindFirstChild("PartNameHere")
1 Like

Yeah you are probably looking for “FindFirstChild().”

Yes, I thought he meant another thing as in the error was saying “part doesn’t exist”.

Another thing, if you wish it to be recursive you can always add the second parameter:

workspace:FindFirstChild("PartNameHere", true)

This will ensure that it will check all across workspace, even if it is inside a model/part.

1 Like