Animation not stopping

Hello!

So here I have this code, which is supposed to make the pet :

  • Follow me
  • Play a walking animation when the player is walking and stop it when the player is idle

The problem :

The only problem is that the pet does play the animation when the player walks, but does not stop. I cannot seem to figure why :frowning:


The code :

local function Follow(pet)
	
	local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
	local BodyPosition = Instance.new("BodyPosition", pet:WaitForChild("Pet"))
	BodyPosition.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
	local BodyGyro = Instance.new("BodyGyro", pet:WaitForChild("Pet"))
	BodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
	
	Character.Humanoid.Running:Connect(function(speed)
		local MovementAnimationTrack = pet:FindFirstChild("AnimationController"):FindFirstChild("Animator"):LoadAnimation(pet:FindFirstChild("Animations"):FindFirstChild("Movement"))
		if speed > 0 then
			MovementAnimationTrack:Play()
			MovementAnimationTrack:AdjustSpeed(2)
		end
		if speed == 0 then
			MovementAnimationTrack:Stop()
		end
	end)
	
	while wait() do
		BodyPosition.Position = HumanoidRootPart.Position + PetPosition
		BodyGyro.CFrame = HumanoidRootPart.CFrame
		pet:WaitForChild("Pet").Orientation = HumanoidRootPart.Orientation + Vector3.new(0,180,0)
	end
	
end

Any help is appreciated!

From what I see, you’re always making a new animation because you put the movement animation in the Running event, this means that you are stopping the animation, but you’re stopping the wrong one as you’re making multiple movement animations

Put the local MovementAnimationTrack line outside of the event and make it so that when the speed is 0, Stop the animation (as you did you here) and set the AnimationTrack’s TimePosition property to 0 so it starts at the beginning when it’s time to move again

1 Like

Tysm Embat! :blush:

Not only you fix my problem, but you also teach me stuff while that! :- D

2 Likes