Error using debounce

I thought I knew how to use the debounce. My problem is that sometimes the debounce is not executed, or that the textbutton can be activated when the debounce should cancel it, what do I do?

local Icon = script.Parent:WaitForChild("Icon")
local isOpen = true 
local waitcache = 0.01
local OpenPosition = UDim2.new(0.85, 0, 0.5, 0)
local ClosePosition = UDim2.new(0.15, 0, 0.5, 0)
local tiempo = 10
script.Parent.MouseButton1Click:Connect(function()
	isOpen = not isOpen 
	if isOpen then
		-- es el de apagado
		isOpen = false
		Icon:TweenPosition(ClosePosition, Enum.EasingDirection.InOut, Enum.EasingStyle.Quint, 0.7, true)
		local TweenService = game:GetService("TweenService")
		local time = 1

		local FadeIn = TweenService:Create(Icon, TweenInfo.new(time), {ImageColor3 = Color3.new(1, 0.537255, 0.537255)})
		FadeIn:Play()
		Icon.Image = "http://www.roblox.com/asset/?id=6652972833"
		wait(tiempo)
		isOpen = true
	else
		--encendido
		isOpen = false
		Icon:TweenPosition(OpenPosition, Enum.EasingDirection.InOut, Enum.EasingStyle.Quint, 0.7, true)
		local TweenService = game:GetService("TweenService")
		local time = 1
		
		local FadeOut = TweenService:Create(Icon, TweenInfo.new(time), {ImageColor3 = Color3.new(0.501961, 1, 0.501961)})
		FadeOut:Play()
		Icon.Image = "http://www.roblox.com/asset/?id=6652973166"
		wait(tiempo)
		isOpen = true
	end
end)

maybe this?
This just inverts value and may result problem

1 Like

Pretty sure this isn’t a debounce
You would need another if statement to wrap the code
Maybe try this?

local Icon = script.Parent:WaitForChild("Icon")
local isOpen = true 
local waitcache = 0.01
local OpenPosition = UDim2.new(0.85, 0, 0.5, 0)
local ClosePosition = UDim2.new(0.15, 0, 0.5, 0)
local tiempo = 10
local Debounce = true

script.Parent.MouseButton1Click:Connect(function()
	if Debounce then
		Debounce = false
		isOpen = not isOpen 
		if isOpen then
			-- es el de apagado
			isOpen = false
			Icon:TweenPosition(ClosePosition, Enum.EasingDirection.InOut, Enum.EasingStyle.Quint, 0.7, true)
			local TweenService = game:GetService("TweenService")
			local time = 1

			local FadeIn = TweenService:Create(Icon, TweenInfo.new(time), {ImageColor3 = Color3.new(1, 0.537255, 0.537255)})
			FadeIn:Play()
			Icon.Image = "http://www.roblox.com/asset/?id=6652972833"
			wait(tiempo)
			isOpen = true
		else
			--encendido
			isOpen = false
			Icon:TweenPosition(OpenPosition, Enum.EasingDirection.InOut, Enum.EasingStyle.Quint, 0.7, true)
			local TweenService = game:GetService("TweenService")
			local time = 1

			local FadeOut = TweenService:Create(Icon, TweenInfo.new(time), {ImageColor3 = Color3.new(0.501961, 1, 0.501961)})
			FadeOut:Play()
			Icon.Image = "http://www.roblox.com/asset/?id=6652973166"
			wait(tiempo)
			isOpen = true
		end
		wait(2)
		Debounce = true
	end
end)
1 Like

Others have already mentioned that you need a debounce to wrap your boolean variable, but you still have issues with changing isOpen, I think this should be good for what you need

local Icon = script.Parent:WaitForChild("Icon")
local isOpen = true 
local waitcache = 0.01
local OpenPosition = UDim2.new(0.85, 0, 0.5, 0)
local ClosePosition = UDim2.new(0.15, 0, 0.5, 0)
local tiempo = 10
local Debounce = true
local TweenService = game:GetService("TweenService")
local time = 1

script.Parent.MouseButton1Click:Connect(function()
	if not Debounce then return end
	
	Debounce = false
	if isOpen then
		-- es el de apagado
		Icon:TweenPosition(ClosePosition, Enum.EasingDirection.InOut, Enum.EasingStyle.Quint, 0.7, true)
		local FadeIn = TweenService:Create(Icon, TweenInfo.new(time), {ImageColor3 = Color3.new(1, 0.537255, 0.537255)})
		FadeIn:Play()
		Icon.Image = "http://www.roblox.com/asset/?id=6652972833"
		wait(tiempo)
	else
		--encendido
		Icon:TweenPosition(OpenPosition, Enum.EasingDirection.InOut, Enum.EasingStyle.Quint, 0.7, true)
		local FadeOut = TweenService:Create(Icon, TweenInfo.new(time), {ImageColor3 = Color3.new(0.501961, 1, 0.501961)})
		FadeOut:Play()
		Icon.Image = "http://www.roblox.com/asset/?id=6652973166"
		wait(tiempo)
	end
	wait(2)
	isOpen = not isOpen 
	Debounce = true
end)

You just need to invert isOpen at the very end along with changing the debounce back to true