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)
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.
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!