How can I test if an object is a child of something?

Hello,
I’m trying to make a touch event, but I have a custom character so sometimes hit.Parent might not work because it might be hit or hit.Parent.Parent. So how can I do this without getting an error?
Thank you,
in advance

1 Like

You could do hit:FindFirstAncestorOfClass()

1 Like

How would you do this for a part name? Sorry for not including that.

for checking if it’s a child of something just do,

if anyinstancehere:FindFirstChild("yourobject") then
    --code here
end

and for the name

if Part.Name == "name here" then
    --code here
end
2 Likes

There are actually many ways in which this can be achieved, which one you choose is entirely up to you:

if child.Parent.Name == "" then --change to name of parent
	--do code
end

if parent:FindFirstChild("") then --change to name of child
	--do code
end

if child:IsDescendantOf() then --change to parent instance
	--do code
end
1 Like