How to check if the parent parent of a object is nil

So for some reason when I do this, it would print out attempt to index nil with parent because the object parent, parent, parent is nil, so I don’t know how to check if the parent, parent, parent isn’t nil.

example

if hit.Parent.Parent.Parent.Parent.Parent then

end

Instance.Parent.Parent .. Parent is rather tedious compared to using Instance:FindFirstAncestor("Anything"). Alternatively, FindFirstAncestorOfClass and FindFirstAncestorWhichIsA are optional.

If Instance.Parent would be still considered, use a recursive function that keeps looking for Instance.Parent until it is nil or another exit condition(for instance, matching instance).

1 Like

wait so, if I did FindFirstChildAncestor, I don’t know what to find, because I’m not trying to find anything since it’s to check if it has a parent.

I’d like to know what the context is when hit.Parent.Parent .. Parent is used. For your information, FindFirstAncestor is similar to FindFirstChild but it’s backwards and look for parent after parent.

ye but

hit:FindFirstAncestor(what do I put in here)

The argument you would set inside the function is the string of matching ancestor’s name. If hit.Parent.Parent.Parent was named “Bob”, use hit:FindFirstAncestor("Bob").

ye but the thing is, that it’s a hitbox and it can touch anything.

What’s the goal of the hitbox though? Is it supposed to hit characters? Players? Objects? You can still add additional checks to avoid false positives before running into an error.

Make it check if its humanoid at first ,then make it check the name u want it to restrict

ye but I want to check if it has a parent parent parent for a reason, it doesn’t matter what it is.

not what I’m looking for though

If the reason is to check if it still exists somewhere in workspace, just check for FindFirstAncestorOfClass("DataModel").

Unless you’re still insisting about using hit.Parent.Parent, gee:

local function getLastAncestorRecursive(object)
	if object.Parent then
		return getLastAncestorRecursive(object.Parent)
	end
	return object
end

Edit: Function wasn’t working as intended due to missing return.

1 Like

ok, I will do that, thanks,