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