Debounce for Combat

I recently made my first combat system but the problem is that player can spam with his attack. So bascially i want to add debounce but when i tried to make my own but it didn’t work well together with ticks and mainly combat system because my timer wasnt working well.

Client side

--Services
local UIS = game:GetService("UserInputService")
local runService = game:GetService("RunService")
local repStorage = game:GetService("ReplicatedStorage")

--Combo related
local punchEvent = repStorage.PunchSignal
local combo = 0
local prevTick = 0
local accTick = 0

--Player
local plr = game:GetService("Players").LocalPlayer
local char = plr.Character

--Animations
local animator: Animator = char:FindFirstChild("Animator", true)

--Main function
local function PunchInput(input: InputObject, processed)
	if processed then return end
	
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		accTick = tick()
		
		local passedTick = accTick - prevTick
		if passedTick < .7 then
			if combo == 1 then
				
				combo = 0
			else
				
				combo += 1
			end
		else
			
			combo = 0
		end
		
		print("tick: "..passedTick)
		
		punchEvent:FireServer(combo) 
		prevTick = tick()
	end
end

UIS.InputBegan:Connect(PunchInput)

Server side

local repStorage = game:GetService("ReplicatedStorage")

local punchEvent = repStorage.PunchSignal

local animations = {
	RightPunch = game.ServerScriptService["Punch System"][" Animations"].RightPunch,
	LeftPunch = game.ServerScriptService["Punch System"][" Animations"].LeftPunch
}

local function Punch(plr: Player, combo)
	local char = plr.Character
	if not char then plr.CharacterAdded:Wait() end
	
	local animator: Animator = char:FindFirstChild("Humanoid").Animator
	
	if combo == 0 then
		
		local rightPunchTrack = animator:LoadAnimation(animations.RightPunch)
		rightPunchTrack:Play()
	elseif combo == 1 then
		
		local leftPunchTrack = animator:LoadAnimation(animations.LeftPunch)
		leftPunchTrack:Play()
	end
end

punchEvent.OnServerEvent:Connect(Punch)

(If something in code is messy or smth just point it)

1 Like