Is There A GetParents/Ascendants() Function?

Good morning dear Devs! I have a question, we all know GetDescendants() (right?), and we all know that if we would try and check (for example) if a Part is in ReplicatedStorage and it’s Parents are 10 Folders and 12 Models (just an example, okay?) we would have to do:

local ParentService = script.Parent.Parent.Parent.Parent —etc.

And thsi would take a lot of time, instead if there is a GetParents/Ascendants() feature this would narrow down to a few lines, so I was asking if it existed (and if it does not please Roblox add this)
If any of you know please reply

You can’t have multiple parents. It would be useful anyway.

I’m not sure if it’s exactly what you’re looking for, but there’s:

  • FindFirstAncestor
  • FindFirstAncestorWhichIsA
  • FindFirstAncestorOfClass

You can use these to simplify your process a tad.

What do you mean? I mean like getting the Parent of the Parent of the Parent of the Parent of the Parent of the Parent

That would still require you to use multiple function calls lol.

But doesn’t FindFirstAncestor and it’s variants only get what you are looking for?

Yes, but presumably if you’re looking for a parent of a parent of a parent, you likely have a specific check in mind.

If you’re truly looking to obtain EVERY parent (e.g. the entire hierarchy), you’ll have to use some form of loop with .Parent + store each parent in a table until hitting the workspace or a main service.

1 Like

I took a look to see if in the Roblox Documentation and it seems that both does not exist + There are other types for the word “Ancestor”:

I believe what you could do is create a script with the variable being the class “ReplicatedStorage”, next use the part for _, in pairs() do to the folder you would like to find, if statement to know if the Part is a BasePart and contains this name… It should be like this:

local ReplicatedStorage = script.Parent
local Folder = ReplicatedStorage.Folder1 -- Folder we want to find the “Part”.

for _, values in pairs(Folder:GetChildren()) do
    for _, model in pairs(values) do
        if model:IsA(“BasePart”) and model.Name == “Part” then
        print(“You have found it!”)
    end
end

Everyone can correct me if the code above is wrong (I did it only my phone at the moment and in a little rush).

1 Like

From my little understanding of scripting this should work, and it’s pretty much strange that this function doesn’t exist, I can’t make a #feature-requests, maybe a Regular is gonna do it…

Why would you need to have a list of specific ancestors? May you provide a specific use case? This doesn’t sound like something you should be doing when setting up your project correctly.

If you just want to get the service something is in, you can simply get the full name of the instance (via GetFullName) and decipher the service from there.

1 Like

I’m not sure if I’m able to give a very specific use case but one I think would be is:
I’m trying to check we’re a Part exactly is, as for example under ReplicatedStorage I have two Parts with the same Name, and I want to know exactly in which Part my Part is

How would getting the parents of the instance be beneficial here?

You can already know which part is the part you want my checking whether they are equal to each other:

local MyPart = --> your part

local OtherPart = --> another part

print(myPart == OtherPart) --> true if they're the same part, false otherwise

Again, you can just get the ancestry by doing Instance:GetFullName().

2 Likes

Thanks, that’s what I was looking for

Been a while, however it still needs a solution so I’ll just write up a function now.

function GetAncestorsOfItem(subject)
	local ancestors = {}
	for index, item in game:GetDescendants() do 
		if subject:IsAncestorOf(item) then 
			ancestors[#ancestors+1] = item --[[ I prefer to use "table[#table+1] =" instead of table.insert(table, item)  ]]
		end
	end
	
	return ancestors
end