Opening and Closing a GuI

These are basic Open/Close functions that can be used to make frame visible

by 647AKI

Author Avatar

Hello! In this tutorial i'll just be explaining how to close and open a GuI. This tutorial will have 2 parts an open button and a close button, and a button that can open/close.

Open button and Close Button:

So first insert a ScreenGui into StarterGui, insert a text button, and a frame. Then insert a text button into the frame

img|125x55

Name the TextButton in the frame 'Close' and the TextButton in the ScreenGui 'Open' Once you've done that, insert a Local Script into each button. We'll be focusing on the close button. To get the input That the user clicked the Button type:

script.Parent.MouseButton1Click:Connect(function()

This can tell the script that the user clicked the script's parent, which is the 'Close' Button. Then to describe the function:

script.Parent.Parent.Visible = false

You'd need to add an extra ' Parent' because we're describing the frame, which is the Parent of the TextButton. You can also do this in many ways, you can describe the Variable at the beginning of the script:

local frame = script.Parent.Parent
local button = script.Parent

This optimizes the script, and can turn long scripts into short ones. Your final script should be

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

Now that we've completed the close Button feel free to test it out also use the 'Output' tool to see if there are any mistakes in script. Now for the Open button Since it's not a member of the frame, you would have to describe the variables at the top:

local frame = script.Parent.Parent.Frame

The script's parent is the 'Open' button and the Open's parent is the ScreenGui, and in the ScreenGui You can find the frame, which we want to make visible again once we've closed it. Same thing as before

script.Parent.MouseButton1Click:Connect(function()

After you've done that, you will do the same as you did for the close button and describe the function

frame.Visible = true

That will make the Frame visible once you click it, you're final script should look like this

local frame = script.Parent.Parent.Frame

script.Parent.MouseButton1Click:Connect(function()
      frame.Visible = true
end)

The GuI should look something like this

img|85x55

Part 2; Open and Close as 1 button

For this one, you can combine it with part 1 of this tutorial. As there are many ways to actually make this button, i've found a way to optimize it from approximately 10 lines, to a short 3 - 4 lines. So do the first step in Part 1, make your button and frame, insert a LOCAL SCRIPT. Make sure it's a localscript or this wont work.

local frame = script.Parent.Parent.Frame

script.Parent.MouseButton1Click:Connect(function()
          frame.Visible = not frame.Visible
end)

and it's that simple!

View in-game to comment, award, and more!