Help with Animator object [SOLVED]

  1. What do you want to achieve? Keep it simple and clear!
    I am making a gun that fires. I want an animation to be played when it does so.
  2. What is the issue? Include screenshots / videos if possible!
    After firing for some time, the Output gets filled with “AnimationTrack limit of 256 tracks for one Animator exceeded, new animations will not be played”
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I did, but none of them seem to help.

Here is relevant code:

function ViewModelThingy.AnimationPlay(AnimationName)
	local ViewModel = workspace.Camera:FindFirstChild("ViewModel")
	local Animator = ViewModel.Humanoid.Animator
	if AnimationName == "Idle" then
		local AnimationToPlay = ViewModel.Animations:FindFirstChild(AnimationName)
		print(AnimationToPlay)
		local AnimationTrack = Animator:LoadAnimation(AnimationToPlay)
		AnimationTrack.Priority = Enum.AnimationPriority.Action
		AnimationTrack:Play()
	elseif AnimationName == "Unholster" then
		local AnimationToPlay = ViewModel.Animations:FindFirstChild(AnimationName)
		print(AnimationToPlay)
		local AnimationTrack = Animator:LoadAnimation(AnimationToPlay)
		AnimationTrack.Priority = Enum.AnimationPriority.Action
		AnimationTrack:Play()
		AnimationTrack.Looped = false
		AnimationTrack.Stopped:Wait()
	else
		local CurrentAnimationsPlaying = Animator:GetPlayingAnimationTracks ( )
		for i,v in ipairs(CurrentAnimationsPlaying) do
			v:Stop()
		end
		local AnimationToPlay = ViewModel.Animations:FindFirstChild(AnimationName)
		print(AnimationToPlay)
		local AnimationTrack = Animator:LoadAnimation(AnimationToPlay)
		AnimationTrack.Priority = Enum.AnimationPriority.Action
		AnimationTrack.Looped = false
		AnimationTrack:Play()
		
		AnimationTrack.Stopped:Connect(function()
			AnimationToPlay = ViewModel.Animations:FindFirstChild("Idle")
			AnimationTrack = Animator:LoadAnimation(AnimationToPlay)
			AnimationTrack.Priority = Enum.AnimationPriority.Action
			AnimationTrack:Play()
		end)
	end
end

Instead of loading the animation to the Animator every time you fire the gun, instead, load the animations in a table, when the item is handed to the player.
(local script)

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local ViewModel = workspace.Camera:WaitForChild("ViewModel") -- Changed this to WaitForChild, so you wont get any errors.
local Animator = ViewModel.Humanoid.Animator

local Animations = {
FireAnimation = Animator:LoadAnimation(AnimId);
IdleAnimation = Animator:LoadAnimation(AnimId);
ReloadAnimation = Animator:LoadAnimation(AnimId);
}

-- In the firing function, use this line to play the animation.
Animations.FireAnimation:Play()

Hmm… Didn’t know that. Will use an ipairs loop to fix the issue. Thanks!
On another note, is there a way to actually “unload” an animation?

As far as i know, there unfortunately isnt a way to unload animations, which i really think there should be.

Welp, rip that idea. Thank you for the help!