AnimationTrack playing incorrect animation

I’m creating an attack for my NPC but for some it’s playing the wrong animation. When it prints “Choosing: Slam Attack” it plays the “Swing Punch” animation. It works fine in the beginning where it plays each of the correct animations once but after that it only performs one of the animation when the print says the other animation name.

local function ChooseAttack()
	
	if choice == 1 then
		currentAnim = SlamAttackAnim
		choice = 2
	else
		currentAnim = SwingPunchAnim
		choice = 1
	end
	
	currentAnim:Play()

end


local function Attack()
	
	if not Attacking then
		
		Attacking = true
		
		ChooseAttack()
		
		print("Choosing: "..tostring(currentAnim.Name))
		
		coroutine.wrap(function()
			for _, Player in pairs(game.Players:GetPlayers()) do
				local Player_Position = Player.Character.HumanoidRootPart.Position
				local Player_Humanoid = Player.Character.Humanoid
				local Golem_Position = Golem.Body.Position
				if (Player_Position - Golem_Position).Magnitude <= 20 then
					wait(currentAnim.Length / 2.5)
					Player_Humanoid:TakeDamage(10)
				end
			end
		end)()
		
		currentAnim.Stopped:Connect(function()
			Attacking = false
		end)
	end
end
1 Like

What is the choice = 2 in your if statement for?

choice = 2 would be the SwingPunchAnim. After it plays SlamAttackAnim it plays SwingPunchAnim and then resets it back.

1 Like

It’s because you’re using else. Since the first if is true, it will ignore the else. You should’ve made another if statement.

if choice == 1 then
	currentAnim = SlamAttackAnim
	choice = 2
end

if choice == 2 then
	currentAnim = SwingPunchAnim
	choice = 1
end

Now it only performs SlamAttackAnim. I’m not sure why it doesn’t switch between the two?

1 Like

Maybe put the currentAnim:Play() in both of the if statements.

1 Like

I’ve tried adding currentAnim:Play() in both if statements before but it still played the wrong one somehow.

1 Like
if choice == 1 then
	currentAnim = SlamAttackAnim
    currentAnim:Play()
	choice = 2
end

if choice == 2 then
	currentAnim = SwingPunchAnim
    currentAnim:Play()
	choice = 1
end

Is it like this?

1 Like

So I figured out the issue. The reason that it kept playing the animation was because the swing or slam punch anim was looped. I tried setting AnimTrack.Looped = false for both in the beginning of the script but it seems it’s likely a bug from Roblox as I had to manually set the looped property to false in the animation editor. Thank you for your help though. I’ll mark yours as a solution.

1 Like