Combatsystem: Hit counter resets in the middle of the combo

Sup, I am making a melee combat system that has 4 m1 and then resets back to 1st m1 after a cooldown, so its similar to popular battleground games like strongest battlegrounds. My problem is, it resets back to 1st m1 like after the 3th m1 but the first cycle work smoothly but after it goes crazy.

here is a ss of the console:
image
(every number represents a attack and it should go like 1,2,3,4 then reset)

here is the server code:

local function playerRenewer(player, lastAttack)
	
	local char = player.Character
	local hum = char.Humanoid
	
	if lastAttack then
		
		playerCounters[player.Name] = 0
		playerLastHit[player.Name] = tick()
		playerReturnWalk[player.Name] = true
		player.Character.combat.canAttack.Value = true
		
		spawn(function()
			
			task.wait(cooldown)
			table.remove(debounceList, table.find(debounceList,player))
			table.clear(hitList)

		end)
		
	else
		
		playerLastHit[player.Name] = tick()
		playerReturnWalk[player.Name] = true
		player.Character.combat.canAttack.Value = true	

		table.remove(debounceList, table.find(debounceList,player))
		table.clear(hitList)
		
	end
	
	spawn(function()
		
		if lastAttack then
			wait(cooldown/2)
		else
			wait(cooldown)
		end

		if returnWalk then

			hum.WalkSpeed = 16
			hum.JumpPower = 50

		end

	end)
	
end


rs.Remotes.Combat.m1.OnServerEvent:Connect(function(player, pressingSpace)
	
	if table.find(debounceList, player.Name) then return end
	if not check.canAttack(player.Character) then return end
	table.insert(debounceList, player.Name)
	
	local char = player.Character
	local root = char.HumanoidRootPart
	local hum = char.Humanoid
	local combat = char.combat
	
	if not playerCounters[player.Name] then
		
		playerCounters[player.Name] = 0
		playerLastHit[player.Name] = tick()
		playerReturnWalk[player.Name] = false
		
	end
	
	combat.canAttack.Value = false
	
	if (tick() - playerLastHit[player.Name]) > cooldown then
		
		print("reseted")
		playerCounters[player.Name] = 0
		
	end
	
	playerCounters[player.Name] = playerCounters[player.Name] + 1
	print(playerCounters[player.Name])
	
	local shove = false
	local smash = false
	local lastAttack = false
	
	playerReturnWalk[player.Name] = false
	
	if playerCounters[player.Name] >= 4 then
		
		if hum.floorMaterial == Enum.Material.Air then --smash		
			smash = true			
		else --shove
			if pressingSpace then
				shove = true
			end
		end
		
		lastAttack = true
		
	end

     --unnecessary code for the solution (but there is called playerRenewer function

end)

if you have additionaly tips to improve it then feel free

1 Like

I think you need to use an identifier. Basically, have a script-wide value that is changed every single time your M1 function is called, and then check if it’s been changed when delaying something. Like this:

Identify = 1

function DoM1()
	--// Whenever this function is called, the value is changed.
	local newID = Identify += 1
	Identify = newID 

	task.wait(4)

	if newID ~= Identify then return end --//Value was changed (most likely due to function being called again)
end