Can't stop an Animation

So I was scripting today a sword and put a looped equipped animation and then when I unequip it should stop the animation but it dosen’t work here is my script also it has no errors:

local remote = script.Parent:WaitForChild("SwordEvent")

remote.OnServerEvent:Connect(function(Player, Value)
	local character = workspace:WaitForChild(Player.Name)
	if Value == "Equipped" then
		print("Equipped")
		
		local lookForAnimation = character:FindFirstChild("EquippedAnimation")
		if lookForAnimation then
			lookForAnimation:Destroy()
		end
		
		local animtion = Instance.new("Animation", character)
		animtion.AnimationId = 	"rbxassetid://6345666840"
		local loader = character:WaitForChild("Humanoid"):LoadAnimation(animtion)
		loader:Play()
	elseif Value == "Unequipped" then
			
		local lookForAnimation = character:FindFirstChild("EquippedAnimation")
		if lookForAnimation then
			local loader = character:WaitForChild("Humanoid"):LoadAnimation(lookForAnimation)
			loader:Stop() --stops animation
			lookForAnimation:Destroy()
		end
		print("Unequipped")
	elseif Value == "Attack" then
		print("Attacked")
	end
end)

Help will be appreciated :slight_smile:

Nevermind my previous post won’t work. I’ll look into this and get back to you later.

You can use this function to loop through currently playing tracks, match the id of the one you want, and stop it if it’s there.
If you don’t care to match it, and are fine stopping all of them, just call :Stop() on each one

Instead of using Humanoid:LoadAnimation(), use Animator:LoadAnimation() because Humanoid:LoadAnimation() is deprecated.

You should probably play this locally. The reason being is animations will still get replicated over so it’s visible by everyone, but also because when you’re firing the remote, you have to go through the annoyance of loading the animation each time the remote is fired. This can lead to glitchy unexpected behaviour, which I don’t think is avoidable.

What you should consider doing is caching your remote, so you’re essentially storing the player, the player’s humanoid, the animation, and the process of loading it, only once. Use a table, if the player isn’t stored within it, insert them, any further remote calls with just reference the stored player and its children. This solves the issue of loading the animation each time the remote is fired.

You could do something like:

local replicatedStorage = game:GetService("ReplicatedStorage") -- I moved your remote to ReplicatedStorage for convenience

local remote = replicatedStorage.SwordEvent
local cacheTable = {}

local animation = Instance.new("Animation") 
animation.AnimationId = "rbxassetid://6345666840"

function cachePlayer(player)
	if cacheTable[player] then return cacheTable[player] end -- If player already exists in table, then find it
	local character = workspace:WaitForChild(player.Name, 6) 
	if character then
		cacheTable[player] = character
        --[[ 
        -- If player doesn't exist within table, set it
        -- Since the remote stores the player, it will only do this once, so you're essentially
        caching the player and it's children because any further remote calls will reference the already
        stored player
        ]]--
        local humanoid = character:WaitForChild("Humanoid", 6) -- conditionally checking humanoid, animator existence. Probably not necessary to do all of this
		if humanoid then
			local animator = humanoid:FindFirstChildOfClass("Animator") -- find animator object
			if animator then
				loader = animator:LoadAnimation(animation) -- load it
			end
		end
		return character
	end
end

Humanoid:LoadAnimation is deprecated, I advice you to use the new method of Animator:LoadAnimation.

All that aside, I’m slightly confused about what your code is doing. You’re referencing two animations, which one are you having issues with? In overview, what I’m suggesting is caching your remote information, by storing the information into a table, and any further calls will just reference it; not re-do. You could do this with your other animation, ‘lookForAnimation’, so it loads it as well within the function above, and destroys if it already exists. Hope this helps! :slight_smile:

remote.OnServerEvent:Connect(function(plr, Value)
	cachePlayer(plr)
	if Value == "Equipped" then
		loader:Play()
        print("Equipped")
	elseif Value == "Unequipped" then
		loader:Stop()
	    print("Unequipped")
	elseif Value == "Attack" then
		print("Attacked")
	end
end)