How to make an ongoing animation stop?

I’m making a pose script and it works fine but, not when the character must return to their normal state (not posing) how do i detect an animation that’s playing inside of the humanoid?

I’m doing the pose animation like this.

> Posing.OnServerEvent:Connect(function(Player,active)
> 	if active == false then
> 	local Character = Player.Character
> 	local Humanoid = Character:WaitForChild("Humanoid")
> 	local HumanoidRP = Character:WaitForChild("HumanoidRootPart")
> 	
> 	local timeStopped = workspace:FindFirstChild("TimeIsStopped")
> 	if timeStopped and not (timeStopped.Value == Player.Name) then
> 		
> 		Posing:FireClient(Player)
> 		
> 		return end
> 		
> 		local wry = Instance.new("Sound")
> 		wry.Volume = .15
> 		wry.SoundId = "rbxassetid://3460343038"
> 		wry.PlaybackSpeed = 1
> 		wry.Parent = game.Workspace
> 		wry:Play()
> 		debris:AddItem(wry,1)
> 		
> 		local randompose = math.random(1,2)
> 		if randompose == 1 then
> 			
> 			local Anim = Instance.new("Animation")
>             Anim.AnimationId = "rbxassetid://4678320414"
>             local Animation = Humanoid:LoadAnimation(Anim)
>             Animation:Play()
> 			
> 		end
> 			
> 	end
> end)

after the function ends i don’t know how to localize the animation that’s ongoing so i can stop it.

Since AnimTracks are Instances, you could utilise an ObjectVale and set it’s .Value to Anim

How would this exactly work? like how would that help it detect the animation the humanoid is playing? sorry if the question comes off as rude

AnimTracks have a IsPlaying property.

Alternatively, you could use Humanoid:GetPlayingAnimationTracks() and iterate through that.

1 Like

do you think this is correct?

for i, track in pairs(Humanoid:GetPlayingAnimationTracks()) do
track:Stop()
end

1 Like

Looks good to me, two things though:

  • If you’re not going to use the index you can just leave an underscore
  • Use ipairs() when iterating over arrays, it’s more optimised and moves in a i++ order.
1 Like