Animation not stopping,

Animation runs on equip, on unequip animation does not stop but it prints silly2, serverscript in serverscriptstorage, no errors

fists["Equip"].OnServerEvent:connect(function(player, LisEquipped)
	print("silly")
	local idle = player.Character.Humanoid:LoadAnimation(fists.Anims["Idle"])
	local isEquipped = player.Stats.isEquipped
	if LisEquipped == true then
		isEquipped.Value = true
		idle:Play()
		print("silly1")
   else 
		isEquipped.Value = false
		idle:Stop()
		print("silly2")
	end
end)

Hi,

this is caused because whenever you unequip the tool, you are stopping a new AnimationTrack, which never played.

To counter this issue, there are multiple ways how can you go about this (I can think of 2). I will try to explain one of them to you.

When you equip the tool, you want to load the animation in (like you do right now) and store the AnimationTrack object inside of a table, which can be accessed when the Player unequips the tool. When you will have this done, you can simply check for the Player’s AnimationTrack in the table you made when the tool was equipped.

This is how I can imagine your fixed code:

local playerIdleAnimations = {}
fists["Equip"].OnServerEvent:Connect(function(player, LisEquipped)
	print("silly")
	local isEquipped = player.Stats.isEquipped
	if LisEquipped == true then
	    local idle = player.Character.Humanoid:LoadAnimation(fists.Anims["Idle"])
        playerIdleAnimations[player] = idle

		isEquipped.Value = true
		idle:Play()
		print("silly1")
   else 
		isEquipped.Value = false

		local idle = playerIdleAnimations[player]
        if idle then
            idle:Stop()
            playerIdleAnimations[player] = nil -- prevent memory leaks
        end
		print("silly2")
	end
end)

I wrote this in the browser, so please excuse any bad formating/mistakes I might’ve made.

Wow, thank you a lot, i really appreciate this. and thank you for explaining it too :pray:

I’m happy to help.

If my answer solves your problem, could you please mark it as a solution? If it doesn’t, please get back to here.

1 Like