How can I get this debouce to work?

  1. What do you want to achieve? A debounce for this shield ability

  2. What is the issue? whatever I tried, none of it worked

  3. What solutions have you tried so far? I’ve tried changing the placing of the debounce around but nothing has worked

here’s my code:

-- services and variables
local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local player = game.Players.LocalPlayer
local character = player.Character
local mouse = player:GetMouse()
local debounce = false
local ShieldOn = ReplicatedStorage.WandaRemotes:FindFirstChild("WandaShieldOn")
local ShieldOff = ReplicatedStorage.WandaRemotes:FindFirstChild("WandaShieldOff")

local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://"

local anim = character.Humanoid:LoadAnimation(animation)

local leftGlow = character.LeftHand.Attachment
local rightGlow = character.RightHand.Attachment

local function toggle(value)
	leftGlow.a.Enabled = value
	leftGlow.b.Enabled = value
	leftGlow.c.Enabled = value
	rightGlow.a.Enabled = value
	rightGlow.b.Enabled = value
	rightGlow.c.Enabled = value
end

-- code
UserInputService.InputBegan:Connect(function(input, gameprocessed)
	if gameprocessed then return end
	if input.KeyCode == Enum.KeyCode.C then
		debounce = true
		anim:Play()
		ShieldOn:FireServer()
	end
end)

UserInputService.InputEnded:Connect(function(input, gameprocessed)
	if gameprocessed then return end
	if input.KeyCode == Enum.KeyCode.C then
		anim:Stop()
		ShieldOff:FireServer()
	end
	wait(4)
	debounce = false
end)

all help is appreciated!

1 Like

Try this:

UserInputService.InputBegan:Connect(function(input, gameprocessed)
	if gameprocessed then return end
	if input.KeyCode == Enum.KeyCode.C and debounce == false then
		debounce = true
		
		anim:Play()
		ShieldOn:FireServer()
		
		task.wait(5) --//Cooldown time
		debounce = false
	end
end)

UserInputService.InputEnded:Connect(function(input, gameprocessed)
	if gameprocessed then return end
	if input.KeyCode == Enum.KeyCode.C then
		anim:Stop()
		ShieldOff:FireServer()
	end
end)

Also, you should always use task.wait() instead of wait(). Here’s why:

1 Like