M1 code occasionaly skips the first M1

I’m making a game that has a M1 system in it and I’m running into an issue where occasionally the an M1 chain starts at the second M1. I’m not really sure what is causing this but I think it might have to do with how the M1’s are reset if none are activated within a period of time.

M1Controller:M1()

if canM1 then
		canM1 = false
		MovementController:ToggleDirectionalMovement(false)
		
		local anim = currentM1Anims[currentM1]
		local animName = "M1_" .. currentM1
		local track = AnimationHandler.LoadAnimation(player.Character, animName, anim, M1Callback).track
		
		StateHandler.SetState("Attacking", true, track.Length)

		task.spawn(function()
			if track.Length ~= 0 then
				if currentM1 < 4 then
					task.wait(track.Length - 0.065)
				else
					task.wait(1.5)
				end
			end
			
			if currentM1 >= 4 then
				currentM1 = 1
			else
				currentM1 += 1
			end
			
			canM1 = true
		end)
		
		task.spawn(function()
			task.wait(track.Length)
			MovementController:ToggleDirectionalMovement(true)
		end)

		lastPunchTime = tick()
	end

Reseting the M1’s if inactive

task.spawn(function()
		while true do
			task.wait(resetTime)
			if lastPunchTime then
				if tick() - lastPunchTime > resetTime then
					currentM1 = 1
				end
			end
		end
	end)

And the UIS connection if needed???

connection = UserInputService.InputBegan:Connect(function(input, gpe)
			if gpe then
				return
			end
			
			if input.UserInputType == Enum.UserInputType.MouseButton1 then
				M1Controller:M1()
			end
		end)

Could you share the code where the animation is being played (via :Play() or similar)?

I fixed it by moving the lastPunchTime check to inside the M1 code itself. All good now!