Debounce cooldown Issue

What is the issue?

I have a script that allows the player to dash and do an explosive ability, I’m trying to make the explosive ability have a cooldown of 5, but if the player does the explosive ability, before the debounce is set to false, the player can’t dash during the cooldown.

Should I make a separate debounce for the explosive ability?

	if input.KeyCode == Enum.KeyCode.Q then
		local state = character:GetAttribute("State")

		if state ~= "Idle" then print("Cannot Evade") return end

		if not debounce then
			debounce = true
			-- debounce
			print("Evade")
			
			animator:LoadAnimation(game.StarterPack.Animations.Dash):Play()
			game.ReplicatedStorage.Events.EvadeEvent:FireServer(state)

			local linearvelocity = Instance.new("LinearVelocity")
			local linearattachment = Instance.new("Attachment")

			linearvelocity.MaxForce = 25000
			linearvelocity.Attachment0 = linearattachment
			linearvelocity.Parent = linearattachment
			linearattachment.Parent = character.HumanoidRootPart
			game.Debris:AddItem(linearattachment,.4)
			local heart = game:GetService("RunService").Heartbeat:Connect(function()
				linearvelocity.VectorVelocity = character.HumanoidRootPart.CFrame.LookVector * 50
			end)

			task.wait(.5)
			heart:Disconnect()
			debounce = false
		end
	end
	
	if input.KeyCode == Enum.KeyCode.T and not debounce then
		debounce = true
		humanoid.WalkSpeed = 5
		animator:LoadAnimation(game.StarterPack.Animations.Explosion):Play()
		combathandler.Fire.Explosion(player)
		humanoid.WalkSpeed = 16
		
		task.wait(5)
		debounce = false
	end
end)

You’ll want to have separate debounce variables for each independent ability. This will act as a cooldown for each.

ie. crouchDebounce and explosiveDebounce

Is there a way where you can use one debounce, so you don’t have to have a bunch of debounce variables?

If you used a single debounce you’d be linking the two abilities together, i.e dashing would also prevent you from using the explosive ability until the dash is off cooldown. Two is necessary for the behavior you’re requesting.

local dashDebounce = false
local explosionDebounce = false

local function doDash()
	if dashDebounce or explosionDebounce then
		return
	end
	dashDebounce = true
	-- dash
	task.wait(.5)
	dashDebounce = false
end

local function doExplosion()
	if explosionDebounce then
		return
	end
	explosionDebounce = true
	-- explosion move
	task.wait(5)
	explosionDebounce = false
end

1 Like

This function might be helpful. It does contains all of the debounce logic inside the call, so that it will automatically trigger the debouncing if it passes. It should only be used as the final check before activating the ability code.

local debounceMap = {}
function debounce(waitTime, key)
	if debounceMap[key] then
		return false
	else
		debounceMap[key] = true
		task.delay(waitTime, function()
			debounceMap[key] = nil
		end)
		return true
	end
end

if debounce(5, "explosion") then
	-- explode code
end

if debounce(2, "dash") then
	-- dash code
end
1 Like