Parent Power Function! :D

Sorry for the small post but this can be useful :smiley:

So I saw on the forums this post: Is there something like parent to the power of? So I decided to make a function for that!

Here it is:

function getParentOfPower (obj,pow)
    local objP = obj

    for i = 1,pow do
        objP = objP.Parent
    end
    
    return objP
end

NOTE: This starts counting from the object. So if you if you wanted to get something parented to the object 3 times then you need to put 3 in the “pow” Argument.

Example usage:

local part = workspace.Part.Part.Part.Part1
print(getParentOfPower(part,3).Name)
 --// prints: "Part" since its parented 3 times to the part instance \\--

Hopefully this helps! :smiley:

4 Likes

You’ll never have a need for this when you can use FindFirstAncestor and the other similar functions or if you set up your hierarchy properly. Just saying. It’d probably be better to use those but in both the case of that and this function, do be mindful to check for nil parents otherwise you might run into an issue with your code (in this case, by attempting to index the Parent of a nil value).

tl;dr Just set up your hierarchy properly. :smiley:

7 Likes