Trying to use MoveTo() after loading an animation

Hello!
I am trying to create a cutscene, and have a group of rigs all in a model. I need them to play an animation to make them kneeling. This animation is on a loop. After the animation has finished, I want them to walk to a specific place. I am using MoveTo() to do this.

Code I am using:

	for i, v in pairs(game.Workspace.WorkMap.Soldiers.GunSoldiers:GetChildren()) do --Loops through all the soldiers and plays their animation
		if v.Name == "Soldier" then
			v.Humanoid:LoadAnimation(v.Kneel):Play()
		end
	end

		for i, v in pairs(game.Workspace.WorkMap.Soldiers.GunSoldiers:GetChildren()) do
			v.Gun.Anchored = false
		 	if v.Name == "Soldier" then
				v.Humanoid:LoadAnimation(v.Kneel):Stop()
                v.Humanoid:MoveTo(game.Workspace.WorkMap.PlayerPositions.SoldierMove1.Position)
			else				
                v.Humanoid:MoveTo(game.Workspace.WorkMap.PlayerPositions.SoldierMove1.Position)
			end
            print("It should have moved by now")
		end

As you can see, I stop the animation, and make the rigs move. I used a print statement to check if the code is actually running. It prints out the message, but the animation does not stop, and the rigs do not move. Is the animation stopping them from moving, and can I fix this in any way?

It’s happening because you’re loading the animation again which is basically creating a new
animation that you’re stopping. To stop the old/aready playing animation you could store
it all in a table that you then loop through and stop all the animations.

Similar to this

local AllAnimations = {}

for _, v in pairs(Npcs) do
	local Animation = v.Humanoid:LoadAnimation(Animation)
	table.insert(AllAnimations, (#AllAnimations + 1), Animation)
	
	Animation:Play()
end

for _, PlayingAnimation in pairs(AllAnimations) do
	PlayingAnimation:Stop()
end
local workmap = workspace:WaitForChild("WorkMap")
local soldiers = workmap:WaitForChild("Soldiers")
local gunsoldiers = soldiers:WaitForChild("GunSoldiers")
local playerpositions = workmap:WaitForChild("PlayerPositions")
local soldiermove1 = playerpositions:WaitForChild("SoldierMove1")

task.wait(5) --slight loading delay

for _, soldier in ipairs(gunsoldiers:GetChildren()) do
	if soldier.Name == "Soldier" then
		local soldierhum = soldier:FindFirstChild("Humanoid")
		if soldierhum then
			local soldieranim = soldier:FindFirstChild("Kneel")
			if soldieranim then
				local soldierkneel = soldierhum:LoadAnimation(soldieranim)
				if soldierkneel then
					soldierkneel:Play()
					soldierkneel.Stopped:Wait()
					local gun = soldier:FindFirstChild("Gun")
					if gun then
						gun.Anchored = false
					end
					soldierhum:MoveTo(soldiermove1.Position)
				end
			end
		end
	end
end

You don’t need two loops for this, just wait for the animation to stop playing for each soldier before unanchoring the soldier’s gun and making them move to a specified location.

I want to put some code in between playing the animation and stopping the animation.

soldierkneel.Stopped:Wait()

You can after this line here.

soldierkneel:Play()
	task.spawn(function()
		--do stuff while animation is playing in here
	end)
soldierkneel.Stopped:Wait()

Do it like this, because if you add yielding code which results in the animation ending before the Stopped event listener is executed then the Stopped event will never fire and any proceeding code will not be executed as :Wait() yields until the event is fired in this particular context.

I used this, but it is still not working. It says that Stop() is not a valid member of the animation.

	local AllAnimations = {}

	for i, v in pairs(game.Workspace.WorkMap.Soldiers.GunSoldiers:GetChildren()) do
		if v.Name == "Soldier" then
			local Animation = v.Humanoid:LoadAnimation(v.Humanoid.Kneel)
			table.insert(AllAnimations, (#AllAnimations + 1), v.Humanoid.Kneel)
			Animation:Play()
		end
	end

--Run other code in here

	for _, PlayingAnimation in pairs(AllAnimations) do
		PlayingAnimation:Stop()
	end

You cant do

v.Humanoid.Kneel

As that’s only an instance holding the animation information, what you need to do is put the
actuall loaded animation inside the table.

local Animation = v.Humanoid:LoadAnimation(v.Humanoid.Kneel)
table.insert(AllAnimations, (#AllAnimations + 1), Animation)

Mark as solution if it’s solved