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

Why do I have to click it multiple times?

*I've been trying to create a *ScreenGUI ** for when the players connect into my game that tells them some basic info, but for it to close the button has to be pressed multiple times and i'm not sure why!

I am a beginner and well..I don't know what to do.

--0-
local WelcomeGUI = true
local Button     = script.Parent
local GUI = script.Parent.Parent.Parent.Frame
--0-

function CloseIt() --Start of the function
	if WelcomeGUI == true then
      GUI:TweenPosition(UDim2.new(0, 400,0, 200),"Out","Bounce")
	wait(0.15)
	WelcomeGUI = false
	elseif WelcomeGUI == false then
      GUI:TweenPosition(UDim2.new(0, 400,0, -200),"Out","Bounce")
	wait(0.15)
	WelcomeGUI = true
	end
end

Button.MouseButton1Down:connect(CloseIt)

2 answers

✓ Accepted answer

The problem is (I believe) related to your use of waits. Instead of using these waits, I would implement a debounce as it is more reliable:

--0-
local WelcomeGUI = true
local Button     = script.Parent
local GUI = script.Parent.Parent.Frame
local Debounce = true
--0-

function CloseIt() --Start of the function
	if not Debounce then return end -- This will exit the function if debounce is false.
	
	Debounce = false
	
	if WelcomeGUI == true then
		GUI:TweenPosition(UDim2.new(0, 400,0, 200),"Out","Bounce")
		WelcomeGUI = false
	else -- You can simply use an 'else' block here rather than an 'elseif'.
		GUI:TweenPosition(UDim2.new(0, 400,0, -200),"Out","Bounce")
		WelcomeGUI = true
	end
	
	wait(1) -- Tweens by default take 1 second.
	Debounce = true
end

Button.MouseButton1Down:connect(CloseIt)

Alternatively, you can use the override argument, like this:

	if WelcomeGUI == true then
		GUI:TweenPosition(UDim2.new(0, 400,0, 200),"Out","Bounce",1,true)
		WelcomeGUI = false
	else
		GUI:TweenPosition(UDim2.new(0, 400,0, -200),"Out","Bounce",1,true)
		WelcomeGUI = true
	end

This will mean that the player doesn't have to wait for the tween to finish before clicking again.

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