Need help with FindFirstAncestorWhichIsA

I need to get a specific model, but there is something that contradicts it:

I need to get 2, but using FindFirstAncestorWhichIsA I am obviously getting “Model”, “goldmine”, so I need to get 2 and I don’t really know how to do that, I tried many approaches:

local model = target:FindFirstAncestorWhichIsA("Model")
print(model:GetFullName())
if model and model:FindFirstChild("OccupiedBy") then
-- stuff
end

I can’t really find a way to get 2 only without .goldmine.Model or .goldmine

1 Like

Is this code in the goldminemodule ModuleScript? If so, why not just do script.Parent.Parent?

2 Likes

It is not inside that script my bad, it is a local script and it’s a “mouse-hover” system

Can’t you just get the .Parent.Parent of the goldmine.Model?

1 Like

as I said above, this code is in a local script:

		target = mouse.Target
		local model = target:FindFirstAncestorWhichIsA("Model")
		print(model:GetFullName())
		if model and model:FindFirstChild("OccupiedBy") then
			clearSelectionPart()
			script.SelectionBox.Adornee = model
		else
			clearSelectionPart()
			script.SelectionBox.Adornee = nil
		end

I don’t understand, I need to get the model when my mouse hovers it that’s why i’m using Ancestor, because I need to find in which model the part is.

So I assume your doing something like a mouse ray or something and you hit an object on a tile represented by a model and you want to get the tile representation model (sounds a bit confusing when I say it like that, sorry).

If you want to just go up two layers looking for models you can do:

local modelLayer1 = target:FindFirstAncestorWhichIsA(“Model”)
local modelLayer2 = modelLayer1:FindFirstAncestorWhichIsA(“Model”)

This would look for a model’s first ancestor model then look for that model’s first ancestor model.

What I think you want though it something that looks for the tile. You can use something called recursion (a function calling its self). One thing you need though is a way to identify which thing is the right one. You can do this in many ways but it looks like you already have a great way to do it: if the model has the child OccupiedBy. This system would look something like this:

[[I can’t comment on mobile]]
local function getTileModel(target)
    local result = target:FindFirstAncestorWhichIsA(“Model”)
    if result:FindFirstChild(“OccupiedBy”) then
        [[If it gets a value it likes it doesn’t repeat]]
        return result
    end
    [[Creates a “recursive loop” until it gets the correct value]]
    return getTileModel(result)
end

This function basically looks through the model ancestors until it finds one with a child named “OccupiedBy”.

Good luck,
PseudoPerson

3 Likes

You’d be wanting a FindLastAncestorWhichIsA - because first ancestor will just return the first ‘Model’ it finds from recursively checking its parents. For example if mouse.Target == hud, the first ancestor that is a model is goldmine.

local function check(object)
    if object.ClassName == 'Model' and object:FindFirstChild('OccupiedBy') then
        return true;
    elseif object == game.Workspace then
        return false; --looped through all parents, didnt get a match so it's dud
    return check(object.Parent);
end

local model = check(mouse.Target)

1 Like

This theoretically works and also practically works, but there is a weird error once I leave the area of the tile with my mouse:

Players.Korrow.PlayerGui.ClientSystem.LocalScript:46: attempt to index nil with 'FindFirstAncestorWhichIsA'

Line 46:
local result = target:FindFirstAncestorWhichIsA("Model")

I understand that it repeats many times and it causes ancestors to be nil already, but trying to fix it i’m getting a stack overflow because of this recursion,…

Nvm, fixed the Recursion stack overflow, just had to move things around:

local function getTileModel(target)
	local result = target:FindFirstAncestorWhichIsA("Model")
	if result then
		if result:FindFirstChild("OccupiedBy") then
			return result
		end
		return getTileModel(result)
	end
end