Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 7 years ago
Edited 7 years ago

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)
0
It doesn't stop the animation when unequipped... BinaryResolved 215 — 7y
0
Oh sorry, I meant the opposite GatitosMansion 187 — 7y
0
Ok one minute BinaryResolved 215 — 7y

1 answer

Log in to vote
1
Answered by 7 years ago

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.

Ad

Answer this question