The problem is, really, that when you unequip the gun whilst reloading, the idle animation does not stop.

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 isEquipped = false
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 
local isInfiniteBullets = true

script.Parent.Equipped:Connect(function()
	isEquipped = true
	mouse.Icon = "rbxassetid://409468479"
	if not isInfiniteBullets then
		script.Parent:WaitForChild("GunUI").Parent = player:WaitForChild("PlayerGui")
	end
	local character = script.Parent.Parent
	humanoid = character:WaitForChild("Humanoid")
	local animations = script.Animations
	--idle anim
	if not idleAnimation then
		idleAnimation = script.Parent.Parent:WaitForChild("Humanoid"):LoadAnimation(animations.Idle)
	end
	idleAnimation:Play()
	
	--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 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

script.Parent.Unequipped:Connect(function()
	isEquipped = false
	print("UNEQUIP!!!!")
	game.StarterPlayer.CameraMinZoomDistance = 0.5
	
	mouse.Icon = ""
	if not isInfiniteBullets then
		player.PlayerGui:WaitForChild("GunUI").Parent = script.Parent
	end

	local currentAnims = humanoid:GetPlayingAnimationTracks()
	for _, v in pairs(currentAnims) do
		print("Stopping anims!")
		v:Stop()
	end
	
	idleAnimation:Stop()
end)

local debounce = false
script.Parent.Activated:Connect(function()	
	if bullets.Value > 0 and isReloading == false and not debounce then
		debounce = true
		
		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)
		if not isInfiniteBullets then
			bullets.Value -= 1
		end
		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)

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


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

2 comments

0

I think the Animation:Stop() function will come handy in this situation.

0

You can also unloop the animation by just simply doing Animation.Loop = false. NOTE: I may be wrong