Tool reload animation stuck when unequipped and equipped

  1. What do you want to achieve?
    For the reload animation of my weapon to not get stuck after being unequipped and equipped again

  2. What is the issue?
    The reload animation of the weapon is stuck if you unequip it and equip it again after reloading.
    I only need to know what part of my localscript in the tool to modify so that this doesn’t happen.

  1. What solutions have you tried so far?
    Adding more booleans, play it or stop it at different parts of the scripts.
-- Localscript for the animations:
local player               = game.Players.LocalPlayer
local userInput            = game:GetService("UserInputService")
local camera               = workspace.CurrentCamera

local function GetMousePosition(X, Y)
	local cameraRay        = camera:ViewportPointToRay(X, Y)
	local newRay           = Ray.new(cameraRay.Origin, cameraRay.Direction * 1000000)
	local target, position = workspace:FindPartOnRay(newRay, player.Character)
	return position
end

local tool                 = script.Parent
local remote               = tool:WaitForChild("RemoteEvent")
local equipped             = false        
local fired                = false
local fireRate             = 0.25
local fireTick             = tick()
local mag                  = 2
local ammo                 = 30
local Ui                   = script.Parent:WaitForChild("Ammo") 
local animationId          = "rbxassetid://18777276654"
local mouseDown            = false
local idleAnim
local reloadanim

local function setupCharacter(character)
	local humanoid         = character:WaitForChild('Humanoid')
	local animator         = humanoid:FindFirstChildOfClass('Animator')

	if not animator then
		return
	end

	idleAnim               = animator:LoadAnimation(script:WaitForChild("IdleAnim"))
	idleAnim.Priority      = Enum.AnimationPriority.Action
	idleAnim.Looped        = true

	reloadanim             = animator:LoadAnimation(script:WaitForChild("ReloadAnim"))
	reloadanim.Priority    = Enum.AnimationPriority.Action4
	reloadanim.Looped      = false
end

local function onCharacterAdded(character)
	setupCharacter(character)

	character:WaitForChild('Humanoid').Died:Connect(function()
		if idleAnim   then idleAnim:Stop() end
		if reloadanim then reloadanim:Stop() end
	end)
end

player.CharacterAdded:Connect(onCharacterAdded)

tool.Equipped:Connect(function()
	equipped                      = true
	if player.Character then
		setupCharacter(player.Character)
		idleAnim:Play()
		screenGui                 = Ui:Clone()
		screenGui.Parent          = player.PlayerGui
		screenGui.TextButton.Text = mag .. "/" .. ammo
	end
end)

tool.Unequipped:Connect(function()
	equipped = false
	if idleAnim then
		idleAnim:Stop()
	end
	if reloadanim then
		reloadanim:Stop()
	end
	if screenGui then
		screenGui:Destroy()
		screenGui = nil
	end
end)

local reloading = false

local function reload()
	if reloading or ammo == 0 or mag > 0 then return end 
	reloading            = true
	game.SoundService.reloadshotgun:Play()

	if screenGui then
		screenGui.TextButton.Text = "Reloading..."
	end

	if reloadanim and equipped then
		reloadanim:Play()
	else
		return
	end

	wait(1.5)

	if ammo >= 2 then
		ammo = ammo - 2
		mag  = 2
	elseif ammo > 0 then
		mag  = ammo
		ammo = 0
	end

	if screenGui then
		screenGui.TextButton.Text = mag .. "/" .. ammo
	end

	reloading = false
end

local function fire()
	while mouseDown and equipped and not reloading and mag > 0 do
		if tick()    - fireTick >= fireRate then
			fireTick = tick()
			mag      = mag - 1
			screenGui.TextButton.Text = mag .. "/" .. ammo

			local mousePos = userInput:GetMouseLocation()
			local position = GetMousePosition(mousePos.X, mousePos.Y)

			local startPos = tool.Handle.Muzzle.WorldPosition

			remote:FireServer(startPos, position)
		end
		wait()
	end
end

userInput.InputBegan:Connect(function(input, gui)
	if not equipped or gui then return end

	if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
		mouseDown = true
		fire()
	elseif input.KeyCode == Enum.KeyCode.R then
		reload()
	end
end)

userInput.InputEnded:Connect(function(input, gui)
	if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
		mouseDown = false
	end
end)

tool.Activated:Connect(function()
	if equipped and not reloading then
		reload()
	end
end)
1 Like

Try adding reloadanim:Stop() before playing idle animation or when equiping tool, because reloadanim priority is higher that idle animation that cause it to overlay it.

:Stop() Can sometimes cause animation to freeze if fadeTime is equal to 0, you can set it at low value to prevent it freezing: AnimationTrack:Stop()

I tried this, nothing different happened, as well as making it reloadanim:Stop(0.1)
I also removed the idleanim:Play after reloading finishes because this idleanim wouldnt stop.