Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How To Make Children Of ScreenGui Available For PlayerGui?

Asked by 4 years ago

I was coding to make a frame appear when clicking on the Store button. Then, I looked up for a tutorial and found a way to make it work. Despite that, I still don't understand this line of code:

local frame = script.Parent.Parent.Frame

Could anyone explain why there must be Parent.Parent but I can't write this code instead: local frame = game.StarterGui.Frame What is Parent.Parent?

1 answer

Log in to vote
2
Answered by 4 years ago

When you create new GUIs in Studio, you create them in StarterGui. Although that is just a kind of storage. When a player joins the game, either in Studio or in the real game, everything inside StarterGui is auomatically duplicated and placed into the player's PlayerGui. So if you want to change something in a Gui while the game is running, you have to aim for the gui located in the player's PlayerGui. Let's say you've got a ScreenGui, and in that some children.

ScreenGui
    FrameOne
    FrameTwo
    FrameThree
    FrameFour
        TextButton
            LocalScript

We can also pretend that the LocalScript is suppose to make FrameThree become invisible, make the Visible property to false, when the TextButton is clicked. Then this would be the code:

script.Parent.MouseButton1Click:Connect(function()
    script.Parent.Parent.Parent.FrameThree.Visible = false
end)

On the first line, script.Parent indicates the TextButton. On the second line, script.Parent.Parent.Parent.FrameThree indicates FrameThree, which is the child of our ScreenGui. Everytime you use script in your code, you mean the specific script you're currently writing in. Then .Parent mean the Parent of your script. So in this case, script.Parent is our TextButton.

So the reason to why you use .Parent and don't go directly to the gui using game.StarterGui is because what is in StarterGui has been duplicated, which means that any changes made to a gui in StarterGui will not affect any player.

StarterGui

0
good explanation 123nabilben123 499 — 4y
0
Thank you Spjureeedd 385 — 4y
0
Thanks for your explanation. Nozo_rapsocooz 16 — 4y
0
No problem Spjureeedd 385 — 4y
Ad

Answer this question