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 6 years ago
Edited 6 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.

1script.Parent.MouseButton1Click:Connect(function()
2 game.ReplicatedStorage.Sound:Play()
3 script.Parent.Parent.Parent.Open.Visible = true
4 script.Parent.Parent.Visible = false
5end)
0
Are you looking for it to make a noise when you close it, or just want it to close? BryanFehr 133 — 6y
0
If you are using the visible property, please make sure that "open" and "script.Parent.Parent" are both NOT screenguis SerpentineKing 3885 — 6y
0
@BryanFehr (im not sure if thats how you reply) yes i want it to only close ImpatientPain 0 — 6y
0
Use Debounce. KardashevScale 110 — 6y
View all comments (2 more)
0
how? ImpatientPain 0 — 6y
0
how? ImpatientPain 0 — 6y

2 answers

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

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

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

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

Ad
Log in to vote
0
Answered by 6 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.

1local 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.

1local 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

1local 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

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

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

1Button.MouseButton1Click:Connect(function()
2  Sound:Play()
3  Open.Visible = true
4  Frame.Visible = false
5end)

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

01local Sound = game.ReplicatedStorage:FindFirstChild("Sound")
02local Button = script.Parent
03local Open = script.Parent.Parent.Parent:FindFirstChild("Open")
04local Frame = script.Parent.Parent
05 
06Button.MouseButton1Click:Connect(function()
07  Sound:Play()
08  Open.Visible = true
09  Frame.Visible = false
10end)

Without the sound it will be.

1local Button = script.Parent
2local Open = script.Parent.Parent.Parent:FindFirstChild("Open")
3local Frame = script.Parent.Parent
4 
5Button.MouseButton1Click:Connect(function()
6  Open.Visible = true
7  Frame.Visible = false
8end)

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