Animations not playing at a consistent speed

So I’m working on a combo system for my game, but I’m noticing that there’s weird behavior where the animations for the punches will either have a small delay before the next punch or they’ll play faster than they’re supposed to and I’m not sure how to fix it, I’ve tried preloading the animations using ContentProvider on a local script but that doesn’t really seem to fix the issue.

Here’s my code, what could be the issue?

local lastm1 = 0
local cd = false
local animations = script.Parent.Animations

script.Parent.M1.OnServerEvent:Connect(function(player)
	local character = player.Character
	local humanoid = player.Character:FindFirstChild("Humanoid")
	local sound = script.Parent.Swing:Clone()
	sound.Parent = character
	if cd == true then return end
	cd = true
	if tick() - lastm1 > 1 then
		count = 1
	end 


	spawn(function()
		count = count + 1
	end)

	lastm1 = tick()

	if count == 1 then
		local punch1 = humanoid:LoadAnimation(animations.Punch1)
		punch1:Play()
		sound:Play()
		task.wait(.4)
		cd = false
	elseif count == 2 then
		local punch2 = humanoid:LoadAnimation(animations.Punch2)
		punch2:Play()
		sound:Play()
		task.wait(.4)
		cd = false
	elseif count == 3 then
		local punch3 = humanoid:LoadAnimation(animations.Punch1)
		punch3:Play() 
		sound:Play()
		task.wait(.4)
		cd = false
	elseif count == 4 then
		local punchKB = humanoid:LoadAnimation(animations.PunchKB)
		punchKB:Play() 
		sound:Play()
		task.wait(1)
		cd = false
		if count == 4 then
			count = 1
		end

	end
end)```

Do not use task.wait() nor wait() it’s inaccurate, it can take anywhere from 0.4 seconds to 10 seconds depending on your lag. Instead I suggest to wait until the end or until a certain event.

If you’d wait until the animation has ended then it’d look something like:

track.Ended:Wait()

Also if you want to compact the script you could use:

local comboAnims = {animations.Punch1, animations.Punch2, animations.Punch1, animations.PunchKB}
local combo = 1 -- this is basically what you have as 'count'
-- Inside the function:
cd = true
local animTrack = humanoid.Animator:LoadAnimation(comboAnims[combo])
animTrack:Play()
sound:Play()
animTrack.Ended:Wait()
cd = false
combo = (combo % (#comboAnims)) + 1