How Would I Get The First Parent Of A Part?

Hey guys, I would like to know how to get the first parent of a part (even if its inside of another part). FindFirstAncestor works, but you need a name for it.

Do you mean part.Parent?
Or are you talking about the root ancestor?

function getRoot(child)
    local parent = child.Parent

    while parent.Parent.Parent  ~= game do
        parent = parent.Parent
    end
    return parent
end

2 Likes

Okay, thats what I probably need. Imma test it

Wait, I read your thing entirely wrong. You said parent.

That’s my bad. My code does the opposite thing…

this is the first child, not the first parent.

I think OP is trying to find Part2’s first parent (which is MyModel in the example below)

game
    MyModel
        Part1
           Part2    

the code i posted should do that. You’d call getRoot(game.MyModel.Part1.Part2) and it will return MyModel

I saw my mistake after I re-read the topic. However, your code will grab workspace in the current use case. Since most times models are parented to workspace.

Edit: Re-read your code. It’s a little confusing with all the Parents in there. Why not just check that the Parent isn’t workspace instead of game?

no worries, and it grabs the model, not workspace.
You can try it out in studio to see for yourself:

I want to get the first parent thats not workspace or the game.

    MODEL

        PART


           PART 2

So in this case I want to get model, even if more than 2 parts.

I want to get the first parent thats not workspace or the game.

Yeah no worries I understand. That’s what the code is doing.
We know that the first parent is going to be in one of the following:
game.Workspace/game.RepliicatedStorage/game.Lighting, etc

so we go up a parent until parent's parent is one of the following:
game.Workspace/game.RepliicatedStorage/game.Lighting, etc

In the example you sent, it’ll go to Part, then it will stop at model because Model’s Parent.Parent is game.

Given game development, and the need for assets to be in workspace to be visible. You can assume that they’re always going to be checking for physical objects in workspace.

function getroot(object, service)
    local service = service or workspace
	object = object.Parent
	
	while object.Parent ~= service do
		object = object.Parent
	end
	
	return object
end
getroot(workspace.Baseplate.Part.Part.Part)
getroot(game.ReplicatedStorage.Model.Part.Part, game.ReplicatedStorage) --Say that we're working in a different service than workspace

Edited your code to do that. It also makes the code look a little less confusing.

Edit: Changed code so you can select what service you want to find the first ancestor of. Even though it will almost always be workspace I’m assuming.

2 Likes

Why would I want to make the player specify the service? An object can’t be in multiple services at once so it’s just making them do more work.

  1. He doesn’t have to specify the service, it will use workspace if they select nothing.
  2. Literally read OP’s messages and he says that it’s a part. So you can assume that it’s going to be in workspace, where it’s visible, and players interact with it, and physics apply to the part.

tl;dr; Royaltoe’s code did that, all the parents in there just makes it seems confusing.

His code will grab the FirstAncestor after the service it’s parented to. So if it’s in workspace it’ll return the model.
If it’s in ReplicatedStorage it’ll still return the model.

use this code:

local function GetFirstParent(Part)
	local Parent = Part.Parent
	repeat
		if Parent.Parent ~= workspace or Parent.Parent ~= game then
			Parent = Parent.Parent
		end
	until Parent.Parent == workspace or Parent.Parent == game
	return Parent
end

print(GetFirstParent(game.Workspace.Chest.Bottom.Model.Part))

hope this help :slight_smile:

2 Likes

definitely the best solution seen and exactly what i’d do myself.

This topic was from 2021.

But also, here’s another possibly less efficient method:

function GetTopParent(instance)
	return instance:FindFirstAncestor(string.split(instance:GetFullName(),'.')[2])
end

Or if you know the parent is a model, you can just use FindFirstAncestorOfClass:

part:FindFirstAncestorOfClass('Model')
1 Like

Definitely like this a lot more, I’ve seen the function before just assumed it was like FindFirstChild where it only searches the child once

edit
just found out the FindFirstChild function has a Recursive option… :man_facepalming:
I have literally been scripting since early 2020 and just found this out :rofl:

2 Likes