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
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.
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).
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.
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
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