Why will my animation not stop

I am making a gun that you hold with two hands, the animation plays when I equip it, but it won’t stop playing when I unequip it.

robloxapp-20200902-1639194.wmv (1.6 MB)

local script

script.Parent.Unequipped:Connect(function(mouse)
	game.ReplicatedStorage.StopHoldingAssaultRifleEvent:FireServer(script.Parent)
	player.PlayerGui.AmmoGui.Enabled = false
	mouse.Icon = nil
end)

server script for equip and unequip

game.ReplicatedStorage.HoldAssaultRifleEvent.OnServerEvent:Connect(function(player, gun)
	local HoldAssaultRifleAnimationTrack = player.Character.Humanoid:LoadAnimation(gun.HoldAssaultRifleAnimation)
	gun.IsEquipped.Value = true
	while true do
		if gun.IsEquipped.Value == true then
			HoldAssaultRifleAnimationTrack:Play()
			wait(30)
		else
			gun.IsEquipped.Value = false
			break
		end
	end
end)

game.ReplicatedStorage.StopHoldingAssaultRifleEvent.OnServerEvent:Connect(function(player, gun)
	local HoldAssaultRifleAnimationTrack = player.Character.Humanoid:LoadAnimation(gun.HoldAssaultRifleAnimation)
	HoldAssaultRifleAnimationTrack:Stop()
end)

Try doing this

´´´
if gun.IsEquipped.Value == true then
HoldAssaultRifleAnimationTrack:Play()
wait(30)
else
gun.IsEquipped.Value = false
HoldAssaultRifleAnimationTrack:Stop()
´´´

Tried it, but it didn’t change anything.

Then try with this:

if gun.IsEquipped.Value == true then
HoldAssaultRifleAnimationTrack:Play()
wait(30)
elseif gun.IsEquipped.Value == false then
HoldAssaultRifleAnimationTrack:Stop()

Idk, it maybe works

why are you waiting on 30 seconds seems to be a bad delay instead just use Animation.Stopped:wait()

game.ReplicatedStorage.HoldAssaultRifleEvent.OnServerEvent:Connect(function(player, gun)
	local HoldAssaultRifleAnimationTrack = player.Character.Humanoid:LoadAnimation(gun.HoldAssaultRifleAnimation)
	gun.IsEquipped.Value = true
	while true do
		if gun.IsEquipped.Value == true then
			HoldAssaultRifleAnimationTrack:Play()
			HoldAssaultRifleAnimationTrack.Stopped:wait() -- then after this code anything you want below it
		else
			gun.IsEquipped.Value = false
			break
		end
	end
end)

I figured it out the problem was that I forgot to set the IsEquipped Value to false when the player unequips the weapon, so it kept playing the animation.