Hello, there are several objects in an object with a long hierarchy, like this
or like this
When I do a deep search, I want it to start from the test file at the beginning and go to the end, and give the result as that object.
Hello, there are several objects in an object with a long hierarchy, like this
or like this
When I do a deep search, I want it to start from the test file at the beginning and go to the end, and give the result as that object.
so like, FindFirstDescendant?
Your wording is insanely confusing what exactly you trying to say, but I think I got it.
:FindFirstChild("Name", true)
will recursively search for a child in all descendants, but the api documentation is not clear about if it does breadth-first or depth-first. If this is important you may want to write your own custom search function.
I think find first descendent is more efficient (potentially by a significant amount if I’m reading how either algorithm works correctly): FindFirstChild vs FindFirstDescendant - #18 by Peyt_n
FindFirstDescendant and FindFirstChild are not what I want. Will only start from the beginning and show the deepest one, but they do not work without putting a name in (). I’m sorry my English level sucks
Ok, yeah I get it now:
function DeepSearch(Object:Instance):Instance
local Deepest
local Deepness = 0 -- ignore these sucky variable names lmao
for _, i in ipairs(Object:GetDescendants()) do
local ancestors = #string.split(i:GetFullName(), ".")
if ancestors <= Deepness then continue end
Deepness = ancestors
Deepest = i
end
return Deepest
end
I slapped this together, no clue If it works since I wrote it from memory and uses a really dumb “trick” to find number of ancestors, I dunno if it’s the most efficient either.
I took your code and tried it. Yes, it’s exactly as I expected. Thank you.
It annoyed me that my script would give incorrect results if any descendant had any dots in its name or anything, so I genuinely made a whole algorithm to use the :GetFullName
thing, until when I finished I sent it over to GPT to improve my comments and variable names, and it pointed out the more obvious solution. (obviously I didn’t copy and paste it’s code and rewrote mine myself)
Anywho, here’s a more correct, yet technically slower script for your goal:
-- Returns a list of all ancestor Instances for the given Object, correctly handling names with dots
function GetAncestors(targetInstance: Instance): {Instance}
local ancestors = {}
local current = targetInstance.Parent
-- Traverse the parent chain until reaching the root (nil)
while current do
table.insert(ancestors, current)
current = current.Parent
end
return ancestors
end
-- Performs a deep search within the given Object to find the Descendant
-- that is nested the deepest (i.e., has the most ancestors).
function DeepSearch(rootObject: Instance): Instance
-- Track the maximum depth found so far
local maxDepth = 0
-- The current candidate for the deepest descendant
local deepestDescendant = rootObject
-- Iterate through every descendant of rootObject
for _, descendant in ipairs(rootObject:GetDescendants()) do
-- Count how many ancestors this descendant has under rootObject
local depth = #GetAncestors(descendant)
-- If this descendant is deeper than any found before, update candidate
if depth > maxDepth then
maxDepth = depth
deepestDescendant = descendant
end
end
return deepestDescendant
end