Query ancestors

With the new QueryDescendants API, I find myself wanting to use the same approach for querying ancestry as well. Just something simple and chill like:

local AncestorModel = Instance:QueryAncestors("Model[$SomeAttribute]")[1]

It would still return an array since potentially there could be multiple ancestors that match. I suppose it should return the ancestors in order from nearest to furthest.

Instead of having to do annoying logic like writing loops:

local AncestorModel = nil
local Current = Start.Parent
while Current do
  if Current:IsA("Model") and Current:GetAttribute("SomeAttribute") then
      AncestorModel = Parent
      break
   end
   Current = Current.Parent
end
5 Likes

Could you please provide use cases when this is needed?

Because I can apply QueryDescdendants and find it useful, but I’ve never felt that QueryAncestors is missed.

Please note, I am not against you request, I am just curious.
Thank you!

QueryDescendants uses the syntax > for children and >> for descendants. Would be neat if it also supported - for siblings, < for parents, and << for ancestors. But it does conflict with the name of the function so that’s an issue and I don’t think splitting it into two functions is helpful.

Having a version of this function that returns a single result would also be nice, such as a QueryFirstDescendant. However, QueryDescendants alone is already incredibly useful so I’m not complaining about what we have currently.

1 Like

Uhhh, it’s pretty common to query ancestry for one reason another. Anything involving spatial queries or which takes arbitrary instances as input will often want to do ancestry lookups. Usually I’m in some kind of situation where I have been given a Part that is the descendant of a Model that is important in the codebase, such as a player character model or a particular defined object type, and I need to obtain that Model without knowing it beforehand.

I don’t care much about siblings, but that syntax for parents would be nice!

You can just do [1] at the end to get the first result, it’s not necessary to have a separate function:

local FirstPart = Model:QueryDescendants("BasePart")[1]
3 Likes

Not really because that would enumerate everything and allocate a table when you really only want the first thing.

3 Likes

Hi, I second this feature request;

I want to be able to query for all of the GuiObject ancestors of an ImageLabel to determine if the ImageLabel itself is visible from any one of it’s GuiObject ancestors’ Visible property and this is exactly the kind of API I need to do it.

Thanks for making the feature request

EDIT: replying to MiniiMann’s reply… (cc @shadowflame63, thanks for the additional ideas/suggestions)

I also find these asks for this feature request valid; perhaps - for siblings, and + to include the instance in the query itself?

EDIT [2]: Just found out that QueryDescendants doesn’t support Content/ContentId properties :frowning:. That’s painful

why do you complicate things?
if you need Gui:IsVisble(Instance) method then ask for it.
Do not harvest meat from hamburgers.