How do I add delay or debounce to my UI toggle on and off script

I have a local script where you can toggle on and off the UI by just pressing button with fade in and fade out effect, however i’m facing a problem where the player can spam it. I want to add a delay or a debounce to it.

For example: Player press the button (It’s “T” in this case) the UI pop up in fade in slowly and then after a while it automatic fade out, it has a delay to prevent player can spamming the key input.

local userInput = game:GetService("UserInputService")

local screenGui = script.Parent
local toggle = false

userInput.InputBegan:Connect(function(key, processed)	
	if key.KeyCode == Enum.KeyCode.T then
		if toggle then
			for i = 1, 0, 0.1 do
				local transparency = NumberSequence.new (i)
				screenGui.Frame.UIGradient.Transparency = transparency
				wait()
			end
			screenGui.Enabled = false
			toggle = false
		else
			for i = 0, 1, 0.1 do
				local transparency = NumberSequence.new (i)
				screenGui.Frame.UIGradient.Transparency = transparency
				wait()
			end
			screenGui.Enabled = true
			toggle = true
		end
		toggle = not toggle 
	end
end)
1 Like

If I read this correctly, you want them to be able to hit “T” to fade in a UI element which would eventually fade out, yes?
Something like this will do the trick:

local toggle = false -- outside of our event connection

-- inside our event connection...
if key.KeyCode == Enum.KeyCode.T then
if toggle then return end
toggle = true
-- do stuff
toggle = false
end
1 Like

You have to make the cooldown one of the conditions that has to be met to run the function.

So where you check for if the key pressed is “T”, also check for debounce.

local userInput = game:GetService("UserInputService")

local screenGui = script.Parent
local toggle = false
local debounce = false --//Create the debounce

userInput.InputBegan:Connect(function(key, processed)	
	if key.KeyCode == Enum.KeyCode.T and debounce == false then --//Does not execute the function if debounce is active
    debounce = true --//Activate cooldown
		if toggle then
			for i = 1, 0, 0.1 do
				local transparency = NumberSequence.new (i)
				screenGui.Frame.UIGradient.Transparency = transparency
				wait()
			end
			screenGui.Enabled = false
			toggle = false
		else
			for i = 0, 1, 0.1 do
				local transparency = NumberSequence.new (i)
				screenGui.Frame.UIGradient.Transparency = transparency
				wait()
			end
			screenGui.Enabled = true
			toggle = true
		end
		toggle = not toggle 
	end
task.wait(1) --//Cooldown time
debounce = false
end)

Edit: optimized the code further.

2 Likes

yes, lemny code already did a good solution, thank you.

thank you this worked however i have one last question, about the fade in, im not sure why but whenever I hit T it did fade appear but disappear not long after, i want to make it fade appear toggle on or off.

I doubt because of the wait() line.

for i = 0, 1, 0.1 do
	local transparency = NumberSequence.new (i)
	screenGui.Frame.UIGradient.Transparency = transparency
        wait()
end

I’m not sure I understand, but so far I know it sounds like the code is being executed twice. Try playing around with the wait times, increase the debounce time, but do not delete the last task.wait for the cooldown timer.

Otherwise I suggest providing a video showcasing the behavior or a detailed description of exactly what you do and what the response is to your action.

local userInput = game:GetService("UserInputService")

local screenGui = script.Parent
local toggle = false
local debounce = false

userInput.InputBegan:Connect(function(key, processed)	
	if key.KeyCode == Enum.KeyCode.T and not processed and not debounce then
		debounce = true

		if toggle then
			for i = 1, 0, -0.1 do
				screenGui.Frame.UIGradient.Transparency = i
				task.wait()
			end
			screenGui.Enabled = false
		else
			for i = 0, 1, 0.1 do
				screenGui.Frame.UIGradient.Transparency = i
				task.wait()
			end
			screenGui.Enabled = true
		end

		toggle = not toggle
		task.wait(3) -- right here you add time.
		debounce = false
	end
end)

1 Like

I see… alr thank you for helping!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.