Animation not stopping for cutscene rig

I am trying to get my cutscene rig to play a running animation while running to its point with Humanoid:MoveTo(), but to stop when reaching the position.
My issue is that upon reaching the position, it prints that it has stopped moving, but the animation just keeps playing after calling animTrack:Stop(). I have also tried doing repeat animTrack:Stop() until not animTrack.IsPlaying but it only stopped it once and acted as if the animation track had stopped playing. Below is a video of the rig running to the point, stopping, and then running to the next point and stopping, and at both stops you can see it doesn’t stop the running animation.

Here is my script, it is a LocalScript located in StarterPlayerScripts.

wait(5)
local plr = game.Players.LocalPlayer
local char = plr.Character
char.Archivable = true
local event = game.ReplicatedStorage.PlayCutscene

local cutsceneProps = workspace.CutsceneProps

event.OnClientEvent:Connect(function(cutsceneId)
	local plrClone = char:Clone()
	local anim = Instance.new("Animation")
	anim.Name = "Anim"
	local animTrack
	
	local runningFunc = false
	local function move(pos)
		if not runningFunc then
			runningFunc = true
			
			local moving = true
			plrClone.Humanoid:MoveTo(pos)

			coroutine.resume(coroutine.create(function()
				plrClone.Humanoid.MoveToFinished:Wait()
				moving = false
				print("stopped moving")
			end))
			while moving do
				print("moving")
				if plrClone.Humanoid.RigType == Enum.HumanoidRigType.R15 then
					anim.AnimationId = "http://www.roblox.com/asset/?id=913376220"
					animTrack = plrClone.Humanoid.Animator:LoadAnimation(anim)
					animTrack:Play()
					wait(animTrack.Length)
				else
					anim.AnimationId = "http://www.roblox.com/asset/?id=180426354"
					animTrack = plrClone.Humanoid.Animator:LoadAnimation(anim)
					animTrack:Play()
					wait(animTrack.Length)
				end
			end
			animTrack:Stop()
			wait()
			runningFunc = false
		end
	end
	
	if cutsceneId == "intro" then
		plrClone.Humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None
		plrClone.Name = "CutsceneRig_"..plr.Name
		plrClone.Humanoid.WalkSpeed = 16
		plrClone.HumanoidRootPart.CFrame = (cutsceneProps.hs.tsPart.CFrame * CFrame.new(Vector3.new(0,0,10))) -- POS1
		
		plrClone.Parent = cutsceneProps.rigs
		wait(1)
		
		move(cutsceneProps.waypoints.intro_1.Position)
		wait(2)
		move(cutsceneProps.waypoints.intro_2.Position)
	end
end)

I have looked at quite a few other posts and it seemed no one had the exact issue as me and none of the solutions for them worked for me. I have also tried debugging quite a bit and trying different ways of stopping the animation. If anyone can help out with my issue, that would be great!

Does a print statement between end and animTrack:Stop() execute?

Yes, it does execute.
(extra characters)

you’re loading a new animation through animTrack = plrClone.Humanoid.Animator:LoadAnimation(anim) every execution, you only need to load once;

loading an animation is periodic, so what’s mostly happening here is that the animation finishes loading and plays after the previous animation has stopped.

1 Like

I actually had it like that before and it didn’t work, but I just tried it again and it works now, so I must have just made a mistake before. The only problem besides that is that it wasn’t immediately stopping due to the wait() but I just turned that into a coroutine and it fixed it. Thank you so much for your help!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.