Getting the last parent

Oi, I’m trying to get the last parent of a part

Can’t use ‘’’FindFirstAncestor()’’’ or ‘’’FindFirstAncestorOfClass”

Because it’s a model inside model situation
And I can’t name every model the same…

FindFirstAncestorOfClass goes by class, not name. “Last” is kind of vague here. Do you mean previous parent, closest parent, or farthest parent? If you mean previous parent you will have to record it yourself inside of a script and keep track of it. If you mean the closest parent, you can just use model.parent. if you mean the farthest parent, that would be either game or workspace.

1 Like

To get the last child of an Instance, you’d need to get the number of children with :GetChildren(), then index it with Instance[amount], to achieve this. Here’s an example:

local lastChild = Instance[#Instance:GetChildren()]
--#Instance:GetChildren() determines the number of children and adds up to the final child.

We use Instance[pos] as the [number] gets the child’s position in the Instance.

1 Like

I’ll try later it thanks!

Busy Right now

Also I want the furthest part from the part

I.e

Door
-handle
—part

I want go get Door (Model) not handle lol

didnt work remember i want the last parent until workspace

Use this epic cool function:

local function GetModel(Part,TargetedParent)
    TargetedParent = TargetedParent or workspace
    local Parent = Part.Parent
    if Parent == game then error("Failed to get model from "..(Part.Name)) end
    return Parent ~= TargetedParent and GetModel(Parent,TargetedParent) or Part
end

All you wanna do is call the function with the first argument being what instance you want to find the upper most parent, by default the uppermost parent would be workspace, if you want to change that just pass in the second argument as the uppermost parent you are looking for.

Here is an example:
We want to find the uppermost parent which is Oh Nice in this case:
image

Now here is how we use the function (note that Oh Nice is parented to workspace)

local function GetModel(Part,TargetedParent)
    TargetedParent = TargetedParent or workspace
    local Parent = Part.Parent
    if Parent == game then error("Failed to get model from "..(Part.Name)) end
    return Parent ~= TargetedParent and GetModel(Parent,TargetedParent) or Part
end

local selection = game.Selection:Get()
print(GetModel(selection[1])) -- this is basically 'workspace.Ohnice.Model.Model.Model.Model.Model.Me'

And that would print Oh Nice, which means the function returns the instance itself.

2 Likes

But wouldn’t Selection work for only plugins and command bar?

This is the basic form.

local parent = script

repeat
    print("Hi", parent.Name)
    parent = parent.Parent
until parent == game.Workspace

print("Done!")

I also provided this in one of my old posts here.

2 Likes

don’t emphasise on that Selection part, I just did it to show that you can give the actual instance without mentioning its uppermost parent anywhere and still getting it.

1 Like

lol im late, i’ve figured it out already, but this works aswell, thanks!