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

How do i make a button close a gui?

Asked by 5 years ago
Edited 5 years ago

I was looking on some tutorials online that show me how to make open and close buttons. I tried a couple of scripts for the close button, but they didn't work. Any help? This is what i got from one of the tutorials.

script.Parent.MouseButton1Click:Connect(function()
 game.ReplicatedStorage.Sound:Play()
 script.Parent.Parent.Parent.Open.Visible = true
 script.Parent.Parent.Visible = false
end)


0
Are you looking for it to make a noise when you close it, or just want it to close? BryanFehr 133 — 5y
0
If you are using the visible property, please make sure that "open" and "script.Parent.Parent" are both NOT screenguis SerpentineKing 3885 — 5y
0
@BryanFehr (im not sure if thats how you reply) yes i want it to only close ImpatientPain 0 — 5y
0
Use Debounce. KardashevScale 110 — 5y
View all comments (2 more)
0
how? ImpatientPain 0 — 5y
0
how? ImpatientPain 0 — 5y

2 answers

Log in to vote
0
Answered by
BryanFehr 133
5 years ago

This is an open and close LocalScript to put into the TextButton or ImageButton.

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

If you want it to make sound, you'll have to edit it accordingly.

Ad
Log in to vote
0
Answered by 5 years ago

You need to make sure you are using a LocalScript and make sure you're placing it inside the button which might be your issue.

But let me help you understand the script.

What I would do is first define everything so with the sound.

local Sound = game.ReplicatedStorage:FindFirstChild("Sound")

And make sure Sound is the name of your sound. (I recommend naming it to something different because, if you have multiple in ReplicatedStorage named Sound then it would confuse the script.)

For example if it was named OpenSound I wouid change this to.

local Sound = game.ReplicatedStorage:FindFirstChild("OpenSound")

Therefore if there is only one sound in there named OpenSound it wont get confused.

Also make sure that in the ReplicatedStorage you have a Sound in there at all, although this isn't even required so you remove this portion of the script.

The part where you find the open this has the Open placed inside of the parent's parent of the button and the parent of the Frame

local Open = script.Parent.Parent.Parent:FindFirstChild("Open")

So for example In the StarterGui it has a child which is the ScreenGui and inside of that is the frame that you want being open and closed and inside of that is the Button. So if it's set up like this then your Open is a child of the StarterGui right next to the ScreenGui. But let's say that your frame that you want open and closed is inside of another frame then your Open needs to be in the ScreenGui with the other frame. I don't exactly recommend this positioning and if this is what's breaking your script then look into this.

Let's add the rest of the definitions

local Sound = game.ReplicatedStorage:FindFirstChild("Sound")
local Button = script.Parent
local Open = script.Parent.Parent.Parent:FindFirstChild("Open")
local Frame = script.Parent.Parent

Now that you have everything defined it's much more simpler.

Button.MouseButton1Click:Connect(function()
  Sound:Play()
  Open.Visible = true
  Frame.Visible = false
end)

And if you put your definitions alongside it your final code will come out as.

local Sound = game.ReplicatedStorage:FindFirstChild("Sound")
local Button = script.Parent
local Open = script.Parent.Parent.Parent:FindFirstChild("Open")
local Frame = script.Parent.Parent

Button.MouseButton1Click:Connect(function()
  Sound:Play()
  Open.Visible = true
  Frame.Visible = false
end)

Without the sound it will be.

local Button = script.Parent
local Open = script.Parent.Parent.Parent:FindFirstChild("Open")
local Frame = script.Parent.Parent

Button.MouseButton1Click:Connect(function()
  Open.Visible = true
  Frame.Visible = false
end)

This is my first time doing an explanation like this tell me if you like it and if it's helpful please and thank you!

Answer this question