FindFirstAncestor but better

You can use a name, but I want to use a function on the candidate.

For my case, I want it not to be of name, but any name with the name containing keyphrase “door”. Which I know how to do with find, just not on an iterating upward ancestral chain fashion.

The second case being a function that looks to see if the candidate ancestor is the one to first contain a specific descendant, or have descendant(s) of the required specifics and attributes.

1 Like

So you basically want to use a predicate function? Could you show some code examples and your desired result?

I really dont know what you meant but you can just return the candidate on the function after you did the codes

local function myFunction()
      --Some codes
      return theObject
end
thing:FindFirstAncestor(myFunction())

Just loop through the Parents and run the function.

local function findFirstAncestor(descendant, callback)
	while not callback(descendant) do
		if descendant == game then return nil end
		descendant = descendant.Parent
	end

	return descendant
end

Alternatively, you could try parsing the string returned by GetFullName for this specific case:

local fullName = whatever:GetFullName()
local ancestorName = string.match(fullName, "%.([^%.]*door[^%.]*)%.")
local ancestor = whatever:FindFirstAncestor(ancestorName)
3 Likes