Deep search in any object but long hierarchy

Hello, there are several objects in an object with a long hierarchy, like this
image
or like this
image

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.

1 Like

I took your code and tried it. Yes, it’s exactly as I expected. Thank you.