My gun does not work

Whenever I unequip my gun, the idle animation still plays. I have tried to :Stop() the animation when the tool is unequipped, but that does not work. The problem is, really, that when you unequip the gun whilst reloading, the animation does not stop.

video of problem:
robloxapp-20210711-0047055.wmv (1.2 MB)

My code: (Local script in the tool)

local player = game.Players.LocalPlayer
local animations = script:WaitForChild("Animations")
local configurations = script:WaitForChild("Configuration")
local remotes = script:WaitForChild("Remotes")

local userInterface = script:WaitForChild("UserInterface")

local mouse = player:GetMouse()
local isReloading = false
local contextActionService = game:GetService("ContextActionService")

local bullets = configurations:FindFirstChild("Bullets")
local maxBullets = configurations:FindFirstChild("MaxBullets")
local maxDistance = configurations:FindFirstChild("MaxDistance")
local regularDamage = configurations:FindFirstChild("RegularDamage")
local headDamage = configurations:FindFirstChild("HeadDamage")

local shootEvent = remotes:FindFirstChild("FireGun")

local humanoid 
local reloadAnimation
local recoil
local idleAnimation 

script.Parent.Equipped:Connect(function()


	mouse.Icon = "rbxassetid://409468479"
	script.Parent:WaitForChild("GunUI").Parent = player:WaitForChild("PlayerGui")


	local character = script.Parent.Parent
	humanoid = character:WaitForChild("Humanoid")

	local animations = script.Animations
	
	--idle anim
	idleAnimation = humanoid:LoadAnimation(animations.Idle)
	idleAnimation.Priority = Enum.AnimationPriority.Action
	
	if idleAnimation then
		idleAnimation:Play()
	end	
	
	--reload anim
	reloadAnimation = humanoid:LoadAnimation(animations:WaitForChild("Reload"))
	reloadAnimation.Priority = Enum.AnimationPriority.Action
	
	--recoil animation
	recoil = humanoid:LoadAnimation("Recoil")
	recoil.Priority = Enum.AnimationPriority.Action
	
end)

local function reload()
	
	if humanoid and bullets.Value < maxBullets.Value then
		isReloading = true
		
		if idleAnimation then
			idleAnimation:Stop()
		end
		
		if reloadAnimation then
			reloadAnimation:Play()
		end
		
		script.Parent.Handle:WaitForChild("Reload"):Play()
		bullets.Value = maxBullets.Value
		player.PlayerGui:WaitForChild("GunUI").Frame:WaitForChild("GunName").Text = script.Parent.Name
		player.PlayerGui:WaitForChild("GunUI").Frame:WaitForChild("Bullets").Text = tostring(bullets.Value) .. "/" .. tostring(maxBullets.Value)
		wait(1)
		
		isReloading = false
	end
end

contextActionService:BindAction("ReloadThGun", reload, false, Enum.KeyCode.R)


script.Parent.Unequipped:Connect(function()
	print("UNEQUIP!!!!")
	game.StarterPlayer.CameraMinZoomDistance = 0.5
	
	mouse.Icon = ""
	player.PlayerGui:WaitForChild("GunUI").Parent = script.Parent
	
	local humanoid = player.Character:WaitForChild("Humanoid")
	local currentAnims = humanoid:GetPlayingAnimationTracks()
	for _, v in pairs(currentAnims) do
		print("Stopping anims!")
		v:Stop()
	end
	
	if idleAnimation.isPlaying then
		idleAnimation:Stop()
	end
	
	if idleAnimation then
		idleAnimation:Stop()
	end
end)

local debounce = false
script.Parent.Activated:Connect(function()	
	if bullets.Value > 0 and isReloading == false and not debounce then
		debounce = true
		
		if not idleAnimation.isPlaying then
			idleAnimation:Play()
		end
		
		local head = script.Parent.Parent.Head.CFrame.LookVector
		local mouseCalculation = CFrame.new(script.Parent.Parent.Head.Position, mouse.Hit.p).LookVector
		local difference = (head - mouseCalculation)
		
		local ray = Ray.new(script.Parent.Handle.CFrame.Position, (mouse.Hit.p - script.Parent.Handle.Position).unit*maxDistance.Value)
		
		local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
		
		--if difference.magnitude < 1.33 then
			script.Parent.Handle:FindFirstChild("FireSound"):Play()
			if recoil then
				recoil:Play()
				recoil:AdjustSpeed(5)
			end
			shootEvent:FireServer(script.Parent, position, mouse.Target)
			bullets.Value -= 1
			
		player.PlayerGui:WaitForChild("GunUI").Frame:WaitForChild("GunName").Text = script.Parent.Name
		player.PlayerGui:WaitForChild("GunUI").Frame:WaitForChild("Bullets").Text = tostring(bullets.Value) .. "/" .. tostring(maxBullets.Value)
	--	end
		--script.Parent.Main.MuzzleFlash.Enabled = true
		--script.Parent.Main["FlashFX[Light]"].Enabled = true
		
		wait(0.1)
		--script.Parent.Main.MuzzleFlash.Enabled = false
	--	script.Parent.Main["FlashFX[Light]"].Enabled = false
		
		debounce = false
	else
		if script.Parent.Parent.Name ~= "Backpack" then
			reload()
		end
	end
end)

–The animation is looped, but I cannot stop it! Please help!

You already answered your problem. The animation is looped, so you need to stop it from playing infinitely. To do this you can just type;

if idleAnimation.IsPlaying then
idleAnimation:Stop()
end