Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do i stop a looping animation when item is unequiped? Any help?

local player = game.Players.LocalPlayer
local character = player.Character
if not character or not character.Parent then
    character = player.CharacterAdded:wait()
end
local humanoid = character.Humanoid
local animation = script.Parent.Hold
local player = game.Players.LocalPlayer
local note = script.Parent.Parent


function Equiped()
	humanoid:LoadAnimation(animation):Play()
end
function Unequiped()
	humanoid:LoadAnimation(animation):Stop()
end

note.Equipped:connect(Equiped)
note.Unequipped:connect(Unequiped)

humanoid:LoadAnimation(animation):Stop() <------- this doesnt work

2 answers

What you're trying to do on line 16: is loading an Animation then trying to stop an animation that isn't even playing. The way I would model your code would be somewhere along the lines of this:

local player = game.Players.LocalPlayer
local character = player.Character
if not character or not character.Parent then
    character = player.CharacterAdded:wait()
end
local humanoid = character.Humanoid
local animation = script.Parent.Hold
local animationLoaded 
local note = script.Parent.Parent

function Equiped()
	animationLoaded = humanoid:LoadAnimation(animation)
	animationLoaded:Play()
end
function Unequiped()
	if animationLoaded then
		animationLoaded:Stop()
		animationLoaded = nil
	end
end

note.Equipped:connect(Equiped)
note.Unequipped:connect(Unequiped)

Where animationLoaded is declared to be humanoid:LoadAnimation, we are now able to directly access and manipulate that exact animation that is played when equipped.

You my friend are trying to load an animation and stopping that animation, not the one playing. I have had this problem recently too. I use this method:

local animation = character.Humanoid:LoadAnimation(animationObject)

equip()
      animation:Play()
end
unequip()
      animation:Stop()
end

This is not a full script of course but just showing my method. I find it very easy to use and not complicated loading animations during the script, just get it all done at the beginning.

This discussion is preserved as it was. New answers, comments, and votes are closed.