Create a script inside the gui. Put this code in it.
If you want it to open and close:
visible=false
script.Parent.Questions.Answers.Correct.MouseButton1Click:connect(function()
if visible==false then
visible = true
script.Parent.Parent.Visible = true
script.Correct:Play()
elseif visible == true then
visible = false
script.Parent.Parent.Visible = false
end
end)
First of all you don't need the extra "MouseButton1Click:connect" because the first one "MouseButton1Click:connect(function()" is the function that's used when it's clicked. So, the "MouseButton1Click:connect()" doesn't really need to be there.
Debounce is important for this type of stuff when you want something to be closed or opened. Using this we can use the if statement to determine if it's visible or not by setting and determining values.
Let's make your script one line shorter by removing the "local Correct" and just using the code "script.Correct" and put ":Play()" after that.
A common mistake made by a user is that they will think that if you change a gui in starterGui with a script it will change the players. Sadly that is incorrect, you have to use game.Players.LocalPlayer.PlayerGui with a local script or put a script in your gui and change it from there.
If you want it to be removed forever then here's another block of code.
If you want it to be removed:
en=false
script.Parent.Questions.Answers.Correct.MouseButton1Click:connect(function()
if en==false and script.Parent.Parent.Visible == true then
en=true
script.Parent.Parent:Destroy()
script.Correct:Play()
end
end)
"en" stands for enabled, so if it's enabled and the gui is visible to the player it will allow it to be destroyed and play the sound. Thought the script get's destroyed along with it, it will still finish what it has left. If the script is somewhere else then the script is fine, only the gui is affected.
A common mistake made by a user is that they will think that if you change a gui in starterGui with a script it will change the players. Sadly that is incorrect, you have to use game.Players.LocalPlayer.PlayerGui with a local script or put a script in your gui and change it from there.
I hoped this helped you ;) ~Spooksletsky @Spooksletsky
1 comment
Remove line 10 then try it.