Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

What's wrong with this click script?

game.StarterGui.Questions.Answers.Correct.MouseButton1Click:connect(function()
	if script.Parent.Parent.Visible == true then
		script.Parent.Parent.Visible = false
		local Correct = script.Correct:Play()
		script.Correct:Play()
		local enabled = true
	end
end)

game.StarterGui.Questions.Answers.Correct.MouseButton1Click:connect()

When the player clicks the text button, it's supposed to play a sound and disappear. Not working. What's wrong her?

1 comment

0

Remove line 10 then try it.

2 answers

local enabled = true 
local Correct = script.Correct
game.StarterGui.Questions.Answers.Correct.MouseButton1Click:connect(function()
    if script.Parent.Parent.Visible == true and enabled == false then
        script.Parent.Parent.Visible = false
        Correct:Play()
	enabled = true 
    end
end)

game.StarterGui.Questions.Answers.Correct.MouseButton1Click:connect()

Here's something I fixed with your script.

  1. The debounce should be located outside of the function so it can be changed when the player clicks it.

  2. Added debounce to if then statement.

  3. Let correct = the sound the, play the variable 'Correct'.

I'm not sure this solved your problem, but it would have fixed 90-100% of your errors.

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

  2. 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.

  3. 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.

  4. 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)
  1. "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.

  2. 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

This discussion is preserved as it was. New answers, comments, and votes are closed.