Why isn't my debounce working?

Hey everyone! I’ve recently made a keybind but the debounce isn’t working. I’ve tried placing it in different places but nothing works, can someone help me?

here’s my code:

local UserInputService = game:GetService("UserInputService")

local player = game.Players.LocalPlayer
local character = player.Character
local Magic = character:FindFirstChild("Magic")
local Hope = character:FindFirstChild("Hope")

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

local RemoteEvent = script:WaitForChild("RemoteEvent")
local debounce = false

UserInputService.InputBegan:Connect(function(input, gameprocessed)
	if gameprocessed then return end

	if input.KeyCode == Enum.KeyCode.X then
		debounce = true
		if Hope.Value == true then
			print("is hope")
			if Magic.Value > 200 then
				print("enough magic")
				
				local Anim = character:WaitForChild("Humanoid"):LoadAnimation(Animation)

				Anim:Play()
				RemoteEvent:FireServer()
				
			end
		end
	end
	wait(5)
	debounce = false
end)
local debounce = false

UserInputService.InputBegan:Connect(function(input, gameprocessed)
	if debounce then
		debounce = false
		task.wait(5)
		return
	end
	debounce = true
	if gameprocessed then return end
	if input.KeyCode == Enum.KeyCode.X then
		if Hope.Value == true then
			print("is hope")
			if Magic.Value > 200 then
				print("enough magic")
				local Anim = character:WaitForChild("Humanoid"):LoadAnimation(Animation)
				Anim:Play()
				RemoteEvent:FireServer()
			end
		end
	end
end)

You’re not using any if statement to detect if the debounce is true or false. You can use your already built statement of gameprocessed to achieve this.

local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")

local player = Players.LocalPlayer
local character = player.Character
local Magic = character:FindFirstChild("Magic")
local Hope = character:FindFirstChild("Hope")

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

local RemoteEvent = script:WaitForChild("RemoteEvent")
local debounce = false

UserInputService.InputBegan:Connect(function(input, gameprocessed)
	if gameprocessed or debounce then return end

	if input.KeyCode == Enum.KeyCode.X then
		task.delay(5, function()
			debounce = false
		end)
		debounce = true
		if Hope.Value == true then
			print("is hope")
			if Magic.Value > 200 then
				print("enough magic")

				local Anim = character:WaitForChild("Humanoid"):LoadAnimation(Animation)

				Anim:Play()
				RemoteEvent:FireServer()

			end
		end
	end
end)
1 Like