Adding a debounce to my script

Hi all, I’ve tried to add a ‘cooldown’ period so it can’t be mashed and cause the GUI to get stuck in one position, but my script didn’t seem to work.

Here is what I wrote, isClicked represents the debounce function

-- Variables
local Main = script.Parent.Parent.Parent.Main
local Hide = script.Parent.Parent
local Close = script.Parent.Parent.Parent.Close
local Button = script.Parent
local CloseButton = Close.TextButton
local isClicked = false


-- Function
script.Parent.MouseButton1Down:Connect(function()
	if Button.Text == "Hide" then do
			if not isClicked then
				isClicked = true
				Main:TweenPosition(UDim2.new(0.3, 0,0, 0), "Out","Back", 1.75)
				Hide:TweenPosition(UDim2.new(0.782, 0,0.018, 0), "Out","Back", 1.75)
				CloseButton.Text = "Close"
				Close:TweenSizeAndPosition(UDim2.new(0.092, 0,0.076, 0), UDim2.new(0.885, 0,0.018, 0), "Out","Back", 1.75)
				wait (1.5)
				Button.Text = "Show"
				isClicked = false

			elseif Button.Text == "Show" then do
					if not isClicked then
						isClicked = true
						Main:TweenPosition(UDim2.new(0, 0,0, 0), "Out","Back", 1.75)
						Hide:TweenPosition(UDim2.new(0.7, 0,0.018, 0), "Out","Back", 1.75)
						Close:TweenSizeAndPosition(UDim2.new(0.177, 0,0.076, 0), UDim2.new(0.8, 0,0.018, 0), "Out","Back", 1.75)
						wait (1.5)
						CloseButton.Text = "Close Ride Panel"
						Button.Text	 = "Hide"
						isClicked = false
					end
				end
			end
		end
	end
end)

Any help is greatly appreciated :smiley:

1 Like

Try this for me and let me know if it worked

-- Variables
local Main = script.Parent.Parent.Parent.Main
local Hide = script.Parent.Parent
local Close = script.Parent.Parent.Parent.Close
local Button = script.Parent
local CloseButton = Close.TextButton
local isClicked = false


-- Function
script.Parent.MouseButton1Down:Connect(function()
	if isClicked then return end
	isClicked = true
	if Button.Text == "Hide" then
		Main:TweenPosition(UDim2.new(0.3, 0,0, 0), "Out","Back", 1.75)
		Hide:TweenPosition(UDim2.new(0.782, 0,0.018, 0), "Out","Back", 1.75)
		CloseButton.Text = "Close"
		Close:TweenSizeAndPosition(UDim2.new(0.092, 0,0.076, 0), UDim2.new(0.885, 0,0.018, 0), "Out","Back", 1.75)
		wait (1.5)
		Button.Text = "Show"
	elseif Button.Text == "Show" then
		Main:TweenPosition(UDim2.new(0, 0,0, 0), "Out","Back", 1.75)
		Hide:TweenPosition(UDim2.new(0.7, 0,0.018, 0), "Out","Back", 1.75)
		Close:TweenSizeAndPosition(UDim2.new(0.177, 0,0.076, 0), UDim2.new(0.8, 0,0.018, 0), "Out","Back", 1.75)
		wait (1.5)
		CloseButton.Text = "Close Ride Panel"
		Button.Text	 = "Hide"
	end
	isClicked = false
end)
1 Like

I’ve had to change the wait() to 2 seconds instead of 1.5 to make it work properly, but aside from that it works perfectly as far as I can see.

Thank you!!!

No problem! Happy to help. :slight_smile:

filfilfilfil