How to get all the parents of parents

Basicly i have a part, and I need to find if this part is in a folder, so i need to find parents of the part and see if its in this folder, but i dont know line for that, bit like getchildren() but in reverse

1 Like

i want t o be able to search mutiple parents like, objects parent, parent, parent, like a list of all objects a bit like getchildren

1 Like

Can you tell us what you’re trying to make? There may be a better way to do whatever you’re trying to do.

1 Like
function getAncestors(object)
	local Ancestors = {}
	local currentObject = object
	
	while currentObject.Parent do
		table.insert(Ancestors, currentObject.Parent)
		currentObject = currentObject.Parent
	end
	
	return Ancestors
end

Function returns a table of the objects ancestors, Table[1] being it’s parent

Hold up I’m blind. To check if an object is a descendant of another object, I believe you can just use part:IsDescendantOf(folder)

So if you have an object like workspace.a
You can do
workspace.a:IsDescendantOf(workspace) which should return true

1 Like

did u need

:FindFirstAncestor()

?

1 Like

You could use that but IsDescendantOf is more straight forward.

1 Like

It all depends on the case, as far as I understand, it needs to determine the most important parent of the element, IsDescendantOf(ancestor: Instance) will only work if we know the name of the parent, and here we need to find it from scratch.

if he means a parent whose name we know, both options will be correct. So yes, I agree with you.

He dos say he wants to find if a part is in a folder, so I guessed that he already knew what the folder was, but we’ll see when he responds lol.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.