Add Instance.FindFirstChildWithDescendant

Currently there is no function that easily allows you to get the model containing a part, even if the part is inside x number of sub models. For example, clicking the hat part on a character to get the character model

It would be useful if this function was built into the engine, so I can do workspace:FindFirstChildWithDescendant(RaycastResult.Instance)

function Module.FindFirstChildWithDescendant(Parent, Descendant, CutoffAt)
	local CheckParent = Descendant.Parent
	
	repeat
		if (CheckParent == Parent) then
			-- Search success
			
			return Descendant
		end
		
		Descendant = CheckParent
		CheckParent = CheckParent.Parent
	until not CheckParent or CheckParent == CutoffAt
	
	-- Search failed
end
2 Likes

You can do RaycastResult.Instance:FindFirstAncestorOfClass("Model"), if you wanted to go through sub models, you can use it recursively in a loop.

I’m wondering when Instance:FindFirstDescendant() will be enabled, Instance:FindFirstAncestor() is currently enabled. I suppose you can just use Instance:FindFirstChild(“Example”, true) but just using the descendant function would be nicer.

1 Like

This won’t work because it might not always be a model. It might be a folder, accessory, worldmodel etc

1 Like

Well if you know what the name is going to be, you can use Instance:FindFirstAncestor(name).

1 Like

It would be nice to have all of these:

Instance:FindFirstChildDescendantWhichIsA()
Instance:FindFirstChildWithDescendantWhichIsA()
Instance:FindFirstAncestorWithDescendantWhichIsA()
2 Likes

They have no reason to keep them disabled.

1 Like