How can I check to see if a child exists?

Often times I would like to check to see if a certain child of an object exists, so I can perform the appropriate action.

In this example, I want to make sure the HumanoidRootPart exists. I would expect this to return true if the child is located, and if the child cant be located, return false. But instead of returning false, it just errors instead.

if Player.Character.HumanoidRootPart then

What is the correct way to do this?

3 Likes

You need to use :FindFirstChild()

if Player.Character:FindFirstChild("HumanoidRootPart") then

This method will return the child you’re looking for, what’s special about it is, unlike normally doing Player.Character.HumanoidRootPart if HRM didn’t exist which would error, this method returns nil instead of erroring.

12 Likes

If Player.Character:FindFirstChild(“HumanoidRootPart”) then

If you want to know if the character exists then you should do

if workspace:FindFirstChild(Player.Name) then

2 Likes

you can use if player.Character:FindFirstChild("HumanoidRootPart") then

Or if you want to wait until the HumanoidRootPart exists, try local HumanoidRootPart = player.Character:WaitForChild("HumanoidRootPart")

2 Likes