How to get the Parent of parent using loop

So, I have this TextButton that is a children of multiple frames and I want to get the Instance of the ScreenGui where the TextButton is located into. From what I can think of using the FindFirstAncestor() is the solution but it will only take the Parent of TextButton.

I needed this because it is so messed up and confusing when I’m using multiple .Parent

local Player = game:GetService("Players").LocalPlayer
Buttom =
for i,Gui in pairs(Player.PlayerGui:GetChildren()) do
    if Buttom:IsDescendantOf(Gui) then
        -- Code --
    end
end

This is good but it’ll only give me a boolean.

What I want is the Instance/userdata of that ScreenGui because I’ll use one of the folders inside of it.

like this:

--If ScreenGui found and its an instance
local Folder = theScreenGui:FindFirstChild("Folder")

Can’t you just use FindFirstAncestorWhichIsA, which checks if the direct parents are ScreenGuis, then checks if the parents’ parents are ScreenGuis etc…

label:FindFirstAncestorWhichIsA("ScreenGui")
2 Likes

Yep I think this is what I need. I just need to tune up my script logic. Thanks!

1 Like