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

How to stop animation after tool is unequipped?

Asked by 2 years ago

using_animationTrack is supposed to play only when tool is equipped and when mouse.Button1Down but somehow it still manages to play after I unequip the tool

local tool = script.Parent
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local idle_animation = tool.Animations.idle
local use_animation = tool.Animations.using

local equipped = false
local debounce = false
local canDamage = false

tool.Equipped:Connect(function()
    equipped = true

    local character = tool.Parent
    local humanoid = character.Humanoid

    local idle_animationTrack = humanoid:LoadAnimation(idle_animation)
    local using_animationTrack = humanoid:LoadAnimation(use_animation)
    idle_animationTrack:Play()

    if equipped == true then -- make sure tool is equipped before animation can be played
        mouse.Button1Down:Connect(function()

            canDamage = true
            idle_animationTrack:Stop()
            using_animationTrack:Play()

        end)

        mouse.Button1Up:Connect(function()

            canDamage = false
            using_animationTrack:Stop()
            idle_animationTrack:Play()

        end)
    end

    tool.Unequipped:Connect(function()
        idle_animationTrack:Stop()
    end)
end)
0
Why is the code to stop it inside the `Equipped` body? You should handle this outside of it, same with the variables. TheeDeathCaster 2368 — 2y

Answer this question