AnimationTrack.Stopped not working properly

So I’ve made a combat system with combos:

--// Animation functions

local function AttackOne()
	coroutine.wrap(function()
	AttackTrack1 = script.Parent.Parent.Humanoid:LoadAnimation(Attack1)
	AttackTrack1.Priority = Enum.AnimationPriority.Action
	AttackTrack1:Play()
	end)()
end

local function AttackTwo()
	coroutine.wrap(function()
	AttackTrack2 = script.Parent.Parent.Humanoid:LoadAnimation(Attack2)
	AttackTrack2.Priority = Enum.AnimationPriority.Action
	AttackTrack2:Play()
	end)()
end

local function AttackThree()
	coroutine.wrap(function()
	AttackTrack3 = script.Parent.Parent.Humanoid:LoadAnimation(Attack3)
	AttackTrack3.Priority = Enum.AnimationPriority.Action
	AttackTrack3:Play()
	end)()
end

local function AttackFour()
	coroutine.wrap(function()
	AttackTrack4 = script.Parent.Parent.Humanoid:LoadAnimation(Attack4)
	AttackTrack4.Priority = Enum.AnimationPriority.Action
	AttackTrack4:Play()
end)()
end

local function AnimationOrder()
	if streak == 0 then
		AttackOne()
	elseif streak == 1 then
		AttackTwo()
	elseif streak == 2 then
		AttackThree()
	elseif streak == 3 then
		AttackFour()
	end
end

local function AnimationEnded()
	if streak == 1 then
		AttackTrack2.Stopped:Wait()
		AnimationCool = false
	print("Streak 1 Passed")	
	elseif streak == 2 then
		AttackTrack2.Stopped:Wait()
		AnimationCool = false
	print("Streak 2 Passed")	
	elseif streak == 3 then
		AttackTrack3.Stopped:Wait()
		AnimationCool = false
	print("Streak 3 Passed")	
	elseif streak == 4 then
		AnimationCool = false
	print("Streak 4 Passed")	
	elseif streak == 0 then 
		AnimationCool = false
	print("Streak 0 Passed")	
	end
end

--// Combo System 




tool.Activated:Connect(function()
	if ComboCool == true then return end
	if AnimationCool == true then return end
	AnimationCool = true	
	if tick() - PreviousPunch <= 2 then -- If previous punch was operated in 0.5 seconds,
		if streak >= 4 then -- If streak was continued for 3 times,
			streak = 0 -- change streak to 0.
			ComboCool =	true
			wait(1)
			ComboCool = false	
		end
		streak = streak + 1 
		print(streak)	
	else -- If time from last punching operation was over than 0.5 seconds,
		streak = 0 -- reset your streak.
	end
	PreviousPunch = tick() 
	
	AnimationOrder()
	AnimationEnded()	
	
local EnemyChar = FindPlayer()
	if EnemyChar ~= nil then
	game.ReplicatedStorage.Remotes.TakeDamage:FireServer()			
	end
		
	
end)	

And I want the cooldown between each animation to be the animation’s length
Problem is, AnimationEnded() stops working at “Streak 1 Passed” print and “2” streak print.

Thank you for reading :slight_smile:

This should work

Code
--// Animation functions

local humanoid: Humanoid = script:FindFirstChildOfClass("Humanoid")
local animator: Animator = humanoid:WaitForChild("Animator")

local AttackAnimations = {
	animator:LoadAnimation(Attack1),
	animator:LoadAnimation(Attack2),
	animator:LoadAnimation(Attack3),
	animator:LoadAnimation(Attack4),
}

do -- Set AnimationTracks.Priority to Action
	local Action = Enum.AnimationPriority.Action
	for _, animationTrack: AnimationTrack in ipairs(AttackAnimations) do
		animationTrack.Priority = Action
	end
end

local LastPunch: number = 0
local TIMEOUT = 0.5
local COOLDOWN = 0
local function isConsecutive(): boolean
	return os.clock() - LastPunch <= TIMEOUT + COOLDOWN 
end

local streaks: integer = 0
local function PlayAttackAnimation()
	if isConsecutive() then
		streaks = streaks + 1 > #streaks and 1 or streaks + 1
	else
		streaks = 1
	end

	AttackAnimations[streaks]:Play()
	AttackAnimations[streaks].Stopped:Wait()
	LastPunch = os.clock()
end

--// Combo System

local TakeDamage: RemoteEvent = game.ReplicatedStorage.Remotes.TakeDamage

local Debounce: boolean
tool.Activated:Connect(function()
	if not Debounce then
		Debounce = true
		PlayAttackAnimation()
		if not FindPlayer() then
			TakeDamage:FireServer()
		end
        -- wait(COOLDOWN)
		Debounce = false
	end
end)
1 Like