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

How do I make it stop the animation when it is unequipped?

How do I make it stop the animation when it is unequipped? Please rewrite my script because it is messy, but you don't have too. Thanks for reading.

local animation = Instance.new('Animation')
local debounce = false

function onEquip()
	script.Parent.Handle.UnsheathSound:Play()
end

function onActivate()
	if not debounce then
		debounce = true
		script.Parent.Handle.SlashSound:Play()
		animation.AnimationId = "http://www.roblox.com/asset/?id=129967390"
		local player = game.Players.LocalPlayer.Character
		local animTrack = player.Humanoid:LoadAnimation(animation)
		animTrack:Play()
		script.Parent.Handle.Script.Disabled = false
		wait(0.5)
		animTrack:Stop()
		script.Parent.Handle.Script.Disabled = true
		wait(0.5)
		debounce = false
	end
end

script.Parent.Equipped:connect(onEquip)
script.Parent.Activated:connect(onActivate)

3 comments

0

It doesn't stop the animation when unequipped...

0

Oh sorry, I meant the opposite

0

Ok one minute

1 answer

✓ Accepted answer

It's extremely simple, make it so the animTrack can be accessed across more than one function. We do this by declaring it outside the function onActivate(). Then we create a function to get when the tool is unequipped. Then we use :Stop() on animTrack.

All together now

local animation = Instance.new('Animation')
local debounce = false
local animTrack

function onEquip()
   script.Parent.Handle.UnsheathSound:Play()
end

function onActivate()
    if not debounce then
        debounce = true
        script.Parent.Handle.SlashSound:Play()
        animation.AnimationId = "http://www.roblox.com/asset/?id=129967390"
        local player = game.Players.LocalPlayer.Character
        animTrack = player.Humanoid:LoadAnimation(animation)
        animTrack:Play()
        script.Parent.Handle.Script.Disabled = false
        wait(0.5)
        animTrack:Stop()
        script.Parent.Handle.Script.Disabled = true
        wait(0.5)
        debounce = false
    end
end

function onUnequip()
	animTrack:Stop()
end

script.Parent.Equipped:connect(onEquip)
script.Parent.Unequipped:connect(onUnequip)
script.Parent.Activated:connect(onActivate)

Hope this helps you.

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