Why does my animation not stop playing

I am making a gun that you have to hold with two hands, when I play the animation it works fine, but when I try to stop the animation it keeps playing it even though I stopped it from looping and stopped it from playing.

When I equip the gun, it works.

When I unequip the gun, it doesn’t work.

The scripts

Local Scripts

script.Parent.Equipped:Connect(function(mouse)
	game.ReplicatedStorage.HoldAssaultRifleEvent:FireServer(script.Parent)
end)
script.Parent.Unequipped:Connect(function(mouse)
	game.ReplicatedStorage.StopHoldingAssaultRifleEvent:FireServer(script.Parent)
end)

Server Scripts

game.ReplicatedStorage.HoldAssaultRifleEvent.OnServerEvent:Connect(function(player, gun)
	local HoldAssaultRifleAnimationTrack = player.Character.Humanoid:LoadAnimation(gun.HoldAssaultRifleAnimation)
	HoldAssaultRifleAnimationTrack.Looped = true
	HoldAssaultRifleAnimationTrack:Play()
end)
game.ReplicatedStorage.StopHoldingAssaultRifleEvent.OnServerEvent:Connect(function(player, gun)
	local HoldAssaultRifleAnimationTrack = player.Character.Humanoid:LoadAnimation(gun.HoldAssaultRifleAnimation)
	HoldAssaultRifleAnimationTrack.Looped = false
	HoldAssaultRifleAnimationTrack:Stop()
end)

It is not the same as the AnimationTrack that is being played. Your stopping a new animation track that has not been played before.


When you do;

local myVariable

Your creating a new reference point for an object or value in the enclosed function.


Try loading the animation just outside of the RemoteEvents.

local HoldAssaultRifleAnimationTrack = player.Character.Humanoid:LoadAnimation(gun.HoldAssaultRifleAnimation)

game.ReplicatedStorage.HoldAssaultRifleEvent.OnServerEvent:Connect(function(player, gun)
	HoldAssaultRifleAnimationTrack.Looped = true
	HoldAssaultRifleAnimationTrack:Play()
end)
game.ReplicatedStorage.StopHoldingAssaultRifleEvent.OnServerEvent:Connect(function(player, gun)
	HoldAssaultRifleAnimationTrack.Looped = false
	HoldAssaultRifleAnimationTrack:Stop()
end)

how am i susposed to load the animation outside the remote event when I can’t get the player if I put it outside the remote event.

Ah I overlooked that. Sorry.

Try handling the animations on the Client. It produces more lag when the server handles Animations and the Client replicates player animations for everyone.

Otherwise, do this;

game.ReplicatedStorage.HoldAssaultRifleEvent.OnServerEvent:Connect(function(player, gun)
	local HoldAssaultRifleAnimationTrack = player.Character.Humanoid:LoadAnimation(gun.HoldAssaultRifleAnimation)
	
	HoldAssaultRifleAnimationTrack.Looped = true
	HoldAssaultRifleAnimationTrack:Play()
	
	local function StopRifle()
		HoldAssaultRifleAnimationTrack.Looped = false
		HoldAssaultRifleAnimationTrack:Stop()
		
		functionToDisconnect:Disconnect()
	end
	
	functionToDisconnect = game.ReplicatedStorage.StopHoldingAssaultRifleEvent.OnServerEvent:Connect(StopRifle)
end)

I worked, thanks for the solution.

1 Like