Help adding debounce to gui script

local shop = script.Parent.Parent.Parent.Main


script.Parent.MouseButton1Click:Connect(function()
	if script.Parent.Parent.Parent.Main.Visible == false then

			script.Parent.Parent.Parent.Icon.BackgroundColor3 = Color3.fromRGB(68, 46, 97)

			script.Parent.Parent.Parent.Main.Visible = true
			shop:TweenPosition(
				UDim2.new(0.185, 0, 0.197, 0),
				"Out",
				"Quad",
				1,
				false
			)

		else 
			if script.Parent.Parent.Parent.Main.Visible == true then

				script.Parent.Parent.Parent.Icon.BackgroundColor3 = Color3.fromRGB(32, 73, 134)

				shop:TweenPosition(
					UDim2.new(0.185, 0,1.09, 0),
					"Out",
					"Quad",
					1,
					false
				)
				wait(1)
				shop.Position = UDim2.new(0.185, 0, -0.61, 0)
				script.Parent.Parent.Parent.Main.Visible = false
				
			end
	    end
	end)

I need help adding debounce to this script, it’s bascially a button which turns a gui on and off with tweens.

I would like the time for you to be able to click the button again to be 2 seconds

Try this:

local shop = script.Parent.Parent.Parent.Main
local Debounce = false
local WaitTime = 2

script.Parent.MouseButton1Click:Connect(function()
	if not Debounce then
		Debounce = true
		if script.Parent.Parent.Parent.Main.Visible == false then

			script.Parent.Parent.Parent.Icon.BackgroundColor3 = Color3.fromRGB(68, 46, 97)

			script.Parent.Parent.Parent.Main.Visible = true
			shop:TweenPosition(
				UDim2.new(0.185, 0, 0.197, 0),
				"Out",
				"Quad",
				1,
				false
			)

		else 
			if script.Parent.Parent.Parent.Main.Visible == true then

				script.Parent.Parent.Parent.Icon.BackgroundColor3 = Color3.fromRGB(32, 73, 134)

				shop:TweenPosition(
					UDim2.new(0.185, 0,1.09, 0),
					"Out",
					"Quad",
					1,
					false
				)
				task.spawn(function()
					wait(1)
					shop.Position = UDim2.new(0.185, 0, -0.61, 0)
					script.Parent.Parent.Parent.Main.Visible = false
				end)
			end
		end
		wait(WaitTime)
		Debounce = false
	end
end)
3 Likes