Problem with animations (NPC)

my problem is that my animations are causing like a freezing effect on my NPC between animations. it only seems to happen when i just load in the game and after a while the animations become smooth, just the way i want it. i’m not sure what the problem is but i appreciate any help. :slight_smile:

video highlighting the problem:

animation.AnimationId = "http://www.roblox.com/asset/?id="..animationIDTable["FriendlyRunAnim"]
animTrack = somedudeAnimator:LoadAnimation(animation)
animTrack:Play()

local function onHumanoidDied()
	if connection then
		connection:Disconnect()
	end
end

game.Players.PlayerAdded:Connect(function(p)
	p.CharacterAdded:Connect(function(c)
		local playerHumanoid:Humanoid = c:WaitForChild("Humanoid")
		local hurley:Tool = somedudethatfollowsu:FindFirstChild("hurley")

		if hurley then
			somedudethatfollowsuHumanoid:EquipTool(hurley)
		end

		playerHumanoid.Died:Connect(onHumanoidDied)

		-- Disconnect any existing connection before creating a new one
		if connection then
			connection:Disconnect()
		end

		connection = RS.Heartbeat:Connect(function()
			local mag = (somedudethatfollowsu:WaitForChild("HumanoidRootPart").Position - c:WaitForChild("HumanoidRootPart").Position).Magnitude
			somedudethatfollowsuHumanoid:MoveTo(c:WaitForChild("HumanoidRootPart").Position)

			if mag < 20 then
				if animation then
					if not isAnimating then
						isAnimating = true
						-- Start RaiseHurleyAnim
						if animTrack then
							animTrack:Stop()
							animTrack = nil
						end
						animation.AnimationId = "http://www.roblox.com/asset/?id="..animationIDTable["RaiseHurleyAnim"]
						animTrack = somedudethatfollowsuHumanoid.Animator:LoadAnimation(animation)
						animTrack:Play()
						animTrack:GetMarkerReachedSignal("startNextAnimation"):Connect(function()
							--Transition to AggressiveRunAnim
							if not isTransitioning then
								isTransitioning = true
								animTrack:Stop()
								animation.AnimationId = "http://www.roblox.com/asset/?id="..animationIDTable["AggressiveRunAnim"]
								animTrack = somedudethatfollowsuHumanoid.Animator:LoadAnimation(animation)
								animTrack:Play()
								animTrack.Ended:Connect(function()
									isAnimating = false
									isTransitioning = false
								end)
							end
						end)
					end
				end

			elseif mag >= 20 then
				if animTrack and animTrack.IsPlaying then
					animTrack:GetMarkerReachedSignal("startFriendlyAnimation"):Connect(function()
						animTrack:Stop()
						animation.AnimationId = "http://www.roblox.com/asset/?id="..animationIDTable["LowerHurleyAnim"]
						animTrack = somedudethatfollowsuHumanoid.Animator:LoadAnimation(animation)
						animTrack:Play()

						animTrack:GetMarkerReachedSignal("startFriendlyRun"):Connect(function()
							animTrack:Stop()
							animation.AnimationId = "http://www.roblox.com/asset/?id="..animationIDTable["FriendlyRunAnim"]
							animTrack = somedudethatfollowsuHumanoid.Animator:LoadAnimation(animation)
							animTrack:Play()
						end)

						isAnimating = false
					end)
				end
			end
		end) -- End of RS.Heartbeat:Connect
	end) -- End of p.CharacterAdded:Connect
end) -- End of game.Players.PlayerAdded:Connect

It seems like a lot going on here that its hard to tell exactly what is wrong; however, I can see one potential problem: you are loading the animations within your loop.

This means that you are not loading the animations before you need them but when you need them which takes time to do. I think this could be the cause of why your animation stops (since there would literally be no animation playing for the loading time between animations). It would also explain why it seems to not work only once.

If this is the problem it is an easy fix. Just move your somedudethatfollowsuHumanoid.Animator:LoadAnimation(animation) outside of the loop where the humanoid is first referenced. I see you have many different animations, so I would assign them each to a different local variable instead of your animTrack variable. Then, :Play() and :Stop() those track variables within the loop like you have been doing.