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)

1 comment

0

Why is the code to stop it inside the Equipped body? You should handle this outside of it, same with the variables.