Animations still play after unequipped

I’m trying to make a gun system with a holster

The issue is that when you do it multiple times it still plays the animations when its unequipped
https://gyazo.com/c3514d96eacfffb05644013372d309e2

I don’t know many solutions to this I’ve tried a lot

LOCAL SCRIPT

script.Parent.Unequipped:Connect(function()
	while true do
		wait()
		if IsnEquippe == true then
			for _,anim in pairs(Player.Character.Humanoid.Animator:GetPlayingAnimationTracks()) do
				if anim.Name == "Animation" then
					anim:Stop()
				elseif anim.Name == "Animation1" then
					anim:Stop()
				end
			end
		end
	end
end)

A common error with animations has to do with priority. See Animation Editor | Roblox Creator Documentation

If you have the animation exported by the default priority, sometimes it does not operate properly with programming.

I put it as the highest priority, that might be the problem?

I’m going to assume your full script looks something like:

local plr = game.Players.LocalPlayer();

local Anim = script.MyAnimation;

script.Parent.Equipped:Connect(function()
      local TrackAnim = plr.Character.Humanoid:LoadAnimation(Anim);
      TrackAnim:Play();
end);

Now, your solution relies on :GetPlayingAnimationTracks. This function is now deprecated and no longer works.

Here is a solution for you though.

local plr = game.Players.LocalPlayer();

local Anim = script.MyAnimation; -- Where your animation with an animation id set is located

local CurrentTrackAnim; -- A variable that we will use for later. 
-- The reason for this being here is so it can be accessed every where in our script, not just inside of a scope.

script.Parent.Equipped:Connect(function()
      CurrentTrackAnim = plr.Character.Humanoid:LoadAnimation(Anim); -- Making our track animation and setting CurrentTrackAnim to it.
      CurrentTrackAnim:Play(); -- Playing the animation
end);

script.Parent.Unequipped:Connect(function()
     if CurrentTrackAnim then -- Seing if we have set a track animation
         CurrentTrackAnim:Stop(); -- If we have, stopping that track animation.
     end;
end);

The difference between a track animation and a normal animation is a track animation is what’s returned after we use Humanoid:LoadAnimation(). It is bound to a humanoid and loaded ready for us to use things like TrackAnimation:Play() and TrackAnimation:Stop().

1 Like

Amazing help thank you, but there is a problem where it works but if I pull it out and put it back in very fast a couple times usually on an odd number, it will stay in the position. I’ve tried a debounce but how could I fix this?

this is my code in the SERVER SCRIPT

local PullOutAnimation = script.Parent.Handle.PullOut
local GunPositioningAnimationHold = script.Parent.Handle.GunPosition
local GunPositiongAniTrack;
local PullOutAniTrack;

script.Parent.PistolEvent.OnServerEvent:Connect(function(Player, PlayerMouse, FireTool)
	
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {Player.Character}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

	local raycastResult = workspace:Raycast(script.Parent.Handle.Position, (PlayerMouse - script.Parent.Handle.Position)*300,raycastParams)

	if raycastResult then
		local hitpart = raycastResult.Instance
		local Model = hitpart:FindFirstAncestorOfClass("Model")
		if Model then
			if Model:FindFirstChild("Humanoid") then
				Model.Humanoid.Health -= 50
			end
		end
	end
	
	local FireAnimation = script.Parent.Handle.GunFire
	local FireAniTrack;
	FireAniTrack = Player.Character.Humanoid:LoadAnimation(FireAnimation)
	FireAniTrack:Play()
	
	FireTool:WaitForChild("Handle").Fire:Play()
	script.Parent.Bolt.Attachment.ParticleEmitter.Enabled = true
	script.Parent.Bolt.Attachment.SpotLight.Enabled = true
	wait(0.4)
	script.Parent.Bolt.Attachment.ParticleEmitter.Enabled = false
	script.Parent.Bolt.Attachment.SpotLight.Enabled = false
end)
	
script.Parent.EquippedPistol.OnServerEvent:Connect(function(Playe)
	GunPositiongAniTrack = Playe.Character.Humanoid:LoadAnimation(GunPositioningAnimationHold)
	PullOutAniTrack = Playe.Character.Humanoid:LoadAnimation(PullOutAnimation)
	
	PullOutAniTrack:Play()
	wait(0.8)
	GunPositiongAniTrack:Play()
	script.Parent.Handle.UnHolster.Playing = true
end)

script.Parent.UnEquipped.OnServerEvent:Connect(function()
	while script.Parent.Unequipped do
		wait()
		if GunPositiongAniTrack then
			GunPositiongAniTrack:Stop()
		end
		if PullOutAniTrack then
			PullOutAniTrack:Stop()
		end
	end
end)

Hello!

Well, first I have a question. Does the event:

script.Parent.UnEquipped.OnServerEvent:Connect(function()

Fire every time the tool is unequipped? And if so then why are you making a while loop there?

It should and yes I will remove the while loop
heres the local scripts that fires the server

script.Parent.Unequipped:Connect(function()
	if ReloadingAniTrack then
		ReloadingAniTrack:Stop()
		script.Parent.UnEquipped:FireServer()
	end
end)
local PullOutAnimation = script.Parent.Handle.PullOut
local GunPositioningAnimationHold = script.Parent.Handle.GunPosition
local GunPositiongAniTrack;
local PullOutAniTrack;
local Equiptted= false;

script.Parent.PistolEvent.OnServerEvent:Connect(function(Player, PlayerMouse, FireTool)
	
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {Player.Character}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

	local raycastResult = workspace:Raycast(script.Parent.Handle.Position, (PlayerMouse - script.Parent.Handle.Position)*300,raycastParams)

	if raycastResult then
		local hitpart = raycastResult.Instance
		local Model = hitpart:FindFirstAncestorOfClass("Model")
		if Model then
			if Model:FindFirstChild("Humanoid") then
				Model.Humanoid.Health -= 50
			end
		end
	end
	
	local FireAnimation = script.Parent.Handle.GunFire
	local FireAniTrack;
	FireAniTrack = Player.Character.Humanoid:LoadAnimation(FireAnimation)
	FireAniTrack:Play()
	
	FireTool:WaitForChild("Handle").Fire:Play()
	script.Parent.Bolt.Attachment.ParticleEmitter.Enabled = true
	script.Parent.Bolt.Attachment.SpotLight.Enabled = true
	wait(0.4)
	script.Parent.Bolt.Attachment.ParticleEmitter.Enabled = false
	script.Parent.Bolt.Attachment.SpotLight.Enabled = false
end)

script.Parent.EquippedPistol.OnServerEvent:Connect(function(Playe)
        Equiptted = true -- Now it's equipted

	GunPositiongAniTrack = Playe.Character.Humanoid:LoadAnimation(GunPositioningAnimationHold)
	PullOutAniTrack = Playe.Character.Humanoid:LoadAnimation(PullOutAnimation)
	
	PullOutAniTrack:Play()
	wait(0.8)

        if Equiptted then
	      GunPositiongAniTrack:Play()
	      script.Parent.Handle.UnHolster.Playing = true
        end
end)

script.Parent.UnEquipped.OnServerEvent:Connect(function()
        Equiptted = false; -- Now it's un equippted

	if GunPositiongAniTrack then
		GunPositiongAniTrack:Stop()
	end
	if PullOutAniTrack then
		PullOutAniTrack:Stop()
	end
end)

The issue was after the waited 0.8 seconds if you unequipted then it would play the next animation without stopping it. So when it plays the second animation we make sure it’s actually equipted.

hmm, I inserted it in the script and it didn’t seem to work, no error

hey man I know you’re probably busy, but I’d like some help.

What animation is playing now?

it is still doing the same thing. it keeps playing

https://gyazo.com/75c6f0bc3e6d5df35f35a3a2a332ae99

I found the problem but I’m not sure how to solve it, so when it goes to the serverscript it doesnt print anything or literally do anything. I think the problem is that its not firing right.

Thank you so much, I find the error where I was doing the event it was checking if there was an animation going on, I fixed it thank you for all your help.