Can't stop animation when playing another animation

I can’t stop my idle animation when firing causing the firing animation to be halfway inbetween both. No matter what I try from track2 = nil to track2.Stop() even stopAnimation2. nothing stops the idle animation from playing. Any tips on how to fix this would be great!

Play and stop for animation2 (Idle):

local function playAnimation2()
	if not isPlaying then
		isPlaying = true
		local humanoid = game.Players.LocalPlayer.Character.Humanoid
		if track1 then
			track1:Stop()
		end
		if track2 then
			track2:Stop()
		end
		if track3 then
			track3:Stop()
		end
		local function loopIdleAnimation()
			if isPlaying then
				track2 = humanoid:LoadAnimation(anim2)
				track2.Priority = Enum.AnimationPriority.Action
				track2:Play()
				track2.Stopped:Connect(loopIdleAnimation)
			end
		end
		loopIdleAnimation()
	end
end
local function stopAnimation2()
	isPlaying = false
	if track2 ~= nil then
		track2:Stop()
		track2 = nil
	end
end

PlayAnimation1 (firing):

local function playAnimation1()
	if not isPlaying then
		isPlaying = true
		if canFire and canReload then
			canFire = false
			canReload = false
			local humanoid = game.Players.LocalPlayer.Character.Humanoid
			if ammo > 0 then
				if track4 then
					track4:Stop()
				end
				if track2 then
					track2:Stop()
				end
				if track3 then
					track3:Stop()
				end
				gunshotP()
				boltcomboP()
				while isPlaying and ammo > 0 do
					track1 = humanoid:LoadAnimation(anim1)
					track1.Priority = Enum.AnimationPriority.Action
					track1:Play()
					track1:AdjustSpeed(4 * fireRate)
					task.wait(0.07)
					ammo = ammo - 1
					updateAmmoCount()
					task.wait(fireRate)

					local startPosition = game.Players.LocalPlayer.Character.HumanoidRootPart.Position
					local mouse = game.Players.LocalPlayer:GetMouse()
					local endPosition = mouse.Hit.p
					eventsFolder.WeaponFiredEvent:FireServer(startPosition, endPosition)
					playMuzzleEffect()
				end
				canFire = true
			else
				gunshotS()
				boltcomboS()
				playAnimation4()
			end
			task.wait(fireDelay)
			canReload = true
		end
	end
end
2 Likes

the closest I’ve been able to get was making a isPlayingIdle for the idleLoop but even still during firing you can see the hand fly into the firing animation

1 Like

You are using a deprecated function, that is Humanoid:LoadAnimation(). Use Animator:LoadAnimation() instead.

3 Likes

Edit: Also this ^^^

Is the AnimationPriority of the idle animation set to “Idle”? I’ve had similar issues with animations at Idle priority not stopping before (but maybe that’s just a me issue lol). A simple way of fixing this is to make sure the animation can loop, but simply change the priority to “Action” as well. Stopping it from there on should work properly.

this definitely seemed to help but now Im having issues with stopping the idle animation during the firing animation. If I set it to looping then the firing animation will play over the idle but then loop forever server side, if I make it not a looping animation but just make it replay when the function is fired then it runs fine but there is a small gap where you can see the arm jump into place from going from idle to firing each time it fires which Im unsure how to fix now.

here is the updated script with the advice:

FiringAnimationPlay:

local function playAnimation1()
	if not isPlaying then
		isPlaying = true
		if canFire and canReload then  -- Add a condition to check if the player can fire
			canFire = false -- Prevent firing until delay is over
			canReload = false -- Prevent reloading while firing
			local humanoid = game.Players.LocalPlayer.Character.Humanoid
			local animator = humanoid:FindFirstChild("Animator") or Instance.new("Animator", humanoid)
			if ammo > 0 then
				if track4 then
					track4:Stop()
				end
				if track2 then
					track2:Stop()
				end
				if not track2 then
					track2:Stop()
				end
				if track3 then
					track3:Stop()
				end
				track1 = animator:LoadAnimation(anim1)
				track1.Priority = Enum.AnimationPriority.Action4
				gunshotP()
				boltcomboP()
				while isLooping and isPlaying do
					track1:Play()
					track1:AdjustSpeed(4 * fireRate)
					ammo = ammo - 1
					if ammo < 0 then
						ammo = 0
					end
					updateAmmoCount()
					if ammo == 0 then
						gunshotS()
						boltcomboS()
						stopAnimation1() -- Stop animation when ammo is zero
						return
					end
					task.wait(fireRate)

					-- Get the start position of the laser beam
					local startPosition = game.Players.LocalPlayer.Character.HumanoidRootPart.Position

					-- Fire the LaserFired remote event to notify the server and other clients
					local mouse = game.Players.LocalPlayer:GetMouse()
					local endPosition = mouse.Hit.p
					eventsFolder.WeaponFiredEvent:FireServer(startPosition, endPosition)
					playMuzzleEffect()
				end
				task.wait(fireRate) -- Add a firing delay
				canFire = true -- Allow firing again after the delay
			else
				gunshotS()
				boltcomboS()
				playAnimation4()
			end
			task.wait(fireDelay) -- Add a delay before the player can fire again
			canReload = true -- Allow reloading after the delay
		end
	end
end

IdleAnimationPlay:

local function playAnimation2()
	if not isPlaying then
		isPlaying = true
		local humanoid = game.Players.LocalPlayer.Character.Humanoid
		local animator = humanoid:FindFirstChild("Animator") or Instance.new("Animator", humanoid)
		if track1 then
			track1:Stop()
		end
		if track2 then
			track2:Stop()
		end
		if track3 then
			track3:Stop()
		end
		track2 = animator:LoadAnimation(anim2)
		track2.Priority = Enum.AnimationPriority.Movement
		task.wait()
		track2:Play()
	end
end

the track2:Stop() should be stopping the idle animation but no matter where I put it in the playAnimation1 it doesn’t fully work and stop the animation, and while it isn’t as bad as before there is still a slight jump during the firing animation caused by the idle animation still slightly playing.