Why doesnt this debounce work?

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

  2. What is the issue? the debounce doesnt work and the player can fire the event multiple times

  3. What solutions have you tried so far? none

here’s the script:

-- services, variabled and functions
local UserInputSerice = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local player = game.Players.LocalPlayer
local character = player.Character

local debounce = false

local PBEvent = ReplicatedStorage.PBEvent

local Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://" -- add your AnimationId after the //

local anim = character.Humanoid:LoadAnimation(Animation)

-- code
UserInputSerice.InputBegan:Connect(function(input, gameprocessed)
	if gameprocessed then return end
	if input.KeyCode == Enum.KeyCode.X and debounce == false then
		anim:Play()
		PBEvent:FireServer()
	end
	task.wait(10)
	debounce = false
end)

all help is appreciated

1 Like

It’s because your setting your debounce false outside the if statement.

1 Like

UserInputSerice.InputBegan:Connect(function(input, gameprocessed)
if gameprocessed then return end
if input.KeyCode == Enum.KeyCode.X and debounce == false then
anim:Play()
PBEvent:FireServer()
debounce = true
task.wait(10)
debounce = false
end
end)

It used to be in the if statement, but it still didnt work

Oh nvm , your not setting the debounce to true inside the if statement, also make a wait(your amount of seconds) in the function

1 Like

Wait it’s because debounce remains false. If it remains false, then the debounce won’t work as it just stays false so it would make the debounce in the statement useless. Hope the explaination helps :grinning_face_with_smiling_eyes:

You forgot to set debounce to true.

UserInputSerice.InputBegan:Connect(function(input, gameprocessed)
	if gameprocessed then return end
	if input.KeyCode == Enum.KeyCode.X and debounce == false then
         debounce = true
		anim:Play()
		PBEvent:FireServer()
	end
	task.wait(10)
	debounce = false
end
1 Like