There are plenty of resources, articles and tutorials to help you.
Tool.Unequipped:Connect(function() -- When player unequips tool
Idle:Stop() -- Stop playing the animation
end)
You should also move the animation tracks outside of the function to the top of the script so any function could use the track variables.
E.g:
local Tool = script.Parent
local attack = Tool.Parent.Humanoid:LoadAnimation(script.Attack) -- Load and define the animations
local Idle = Tool.Parent.Humanoid:LoadAnimation(script.Idle)