Is there something like parent to the power of?

So basically, I was wondering if there was anything that could replace stupid long pieces of navigation like this:

local part = descendant.Parent.Parent.Parent.Parent

I was wondering if there was something like the following:

local part = descendant.Parent^4

Thanks for reading, please let me know if you are aware of anything functioning like this.

3 Likes

(post withdrawn by author, will be automatically deleted in 1 hour unless flagged)

No, there isn’t any way to accomplish this. Also I wouldn’t recommend to use .Parent, unless its a UI. Instead do:

local Part = game.Workspace.Part

I do notice your taking .Descendant, I’m guessing that your looping through a table which is a PERFECTLY fine way to use .Parent.

1 Like

Don’t use game.Workspace, use workspace
Also this is terrible because if Part is not a direct parent of workspace and is in, let’s say, a model, you will need to have a unique name for this model. Therefore preventing you from having, let’s say, multiple zombies with the same name.

This is the wrong category, btw: this should’ve been in #help-and-feedback:scripting-support


There is no syntax that supports this, so descendant.Parent^4 wouldn’t work, however, you could use a function for this:

function GetParent(object, index)
    index -= 1
    if index < 0 then
        return object
    else
        return GetParent(object.Parent, index)
    end
end

-- usage
local object = workspace.Baseplate.Script
print( GetParent(object, 0) ) -- Script
print( GetParent(object, 1) ) -- Baseplate
print( GetParent(object, 2) ) -- Workspace
2 Likes

I don’t believe any syntax like that exists. I did find 3 variations of a FindFirstAncestor method, which might be able to help you if you know beforehand the name or class of the ancestor to be found.

FindFirstAncestor
FindFirstAncestorOfClass
FindFirstAncestorWhichIsA

If you just want to get whatever is X levels up in the hierarchy without caring about its information, you’ll need to make a custom function that does that in Lua.