Help with stopping an animation

Hi, I have a sword where when it equips it uses an animation that is looped with the priority of Movement to hold the character’s arm in an upright position.


image


When I try to disequip it, the animation stays.


image


I’ve tried stopping the animation tracks then using a higher priority animation to stop that, but it didn’t work. The higher priority animation only moved the characters arms down for a bit, but then it went back to the above image again.

Code used to stop it:

	local AnimationTracks = Player.Character.Humanoid:GetPlayingAnimationTracks()
	for i, v in pairs(AnimationTracks) do
		v:Stop()
	end
	local AH = Player.AnimationHolder
	AH.AnimationId = Animations.ResetAnimation
	Player.Character.Humanoid:LoadAnimation(AH):Play()
7 Likes

Where exactly is this code located inside the script?

1 Like

It’s a function in a module script. I know the function runs because the sword was removed from my hand.

2 Likes

What you have right here v should stop all playing animations on the character.

	local AnimationTracks = Player.Character.Humanoid:GetPlayingAnimationTracks()
	for i, v in pairs(AnimationTracks) do
		v:Stop()
	end

Is it possible that you’re telling the Holding animation to play again somewhere else in the module script?

1 Like

No, I don’t use while loops for animations since those eventually should break due to the animation limit at about 255. I have it in a local script which is in the sword, but should have been destroyed. It only runs once.

1 Like

Can we see the code for playing the holding animation?


--local PlayerHandAttachment = Player.Character.RightHand.RightGripAttachment
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
--local Animations = require(3689795953)
local AH = Player.AnimationHolder
	
AH.AnimationId = "rbxassetid://3690136072"
Player.Character.Humanoid:LoadAnimation(AH):Play()
	
Mouse.Button1Down:Connect(function()
	print(game.ReplicatedStorage.FireWeapon:InvokeServer(script.Parent, Mouse.hit))
end)


1 Like

Alright so AH.AnimationId = Animations.ResetAnimation should that be Animations.ResetAnimation.Value/Animations.ResetAnimation.AnimationId or what, because that looks off to me.

Animations is a module script which I use to store animation ids since they don’t need to be changed.

2 Likes

Instead of getting the animation tracks using GetPlayingAnimationTracks, why don’t you save the track to a variable or a table and stop it from there? Since everything is indexed, there would be no mystery as to what is being stopped or played.

All you would need to do is determine what animations are played from the table when the tool is equipped/unequipped and reload animations when the humanoid changes. You could probably do this all from a module script to keep it neat and tidy too.

2 Likes

One issue I have is that I play animation tracks all over the place, from modules to local scripts, so this would be difficult

2 Likes

Just to clarify, are you using the Tool.Unequipped with the stopping function?

No, I’m using an accessory then calling :Destroy() on it.

1 Like

Have you tried adding the tracks to the module so that the module can manage them instead? You could make it so your event functions loop through a table of animation tracks that you can edit.

1 Like

Ooof have you tried commenting out

	local AH = Player.AnimationHolder
	AH.AnimationId = Animations.ResetAnimation
	Player.Character.Humanoid:LoadAnimation(AH):Play()

it may look weird not having and idle animation and reset animation, there could be a possibility that the holding animation is somehow getting played again somewhere. (Oh and is the local script getting disabled before destroying that and stopping the animation?)

An issue is I also play animations from the client

1 Like

If I recall correctly animations that are played on the client also replicate to the server.

1 Like
  • The local script is being destroyed before playing the reset animation and the loop
  • I doubt the holding animation is being played again, but I guess I’ll add a wait(1) to see.

A rule that comes from requiring modules is that they run on the datamodel that the requiring script came from. So if you require a module from a local script, then it would have access to client-side instead of server-side.

Yes, but how could I detect this?