Animation Track Won't Stop?

Everything in my scripts works (including the animation) but for some reason the animation track won’t stop when i tell it to.

–Here’s The Script

local UIS = game:GetService("UserInputService")

local weld = Instance.new("Weld")
local swinging = false

local function onSwingEvent(plr)
    local char = plr.Character or plr.CharacterAdded:Wait()
    local hum = char:WaitForChild("Humanoid")
    local animator = hum:WaitForChild("Animator")

    -- Load the animation only once
    local animation = game.ReplicatedStorage:FindFirstChild("SwingAnimation")
    if not animation then
        animation = Instance.new("Animation")
        animation.AnimationId = "rbxassetid://18263356301"
        animation.Name = "SwingAnimation"
        animation.Parent = game.ReplicatedStorage
    end

    local animTrack = animator:LoadAnimation(animation)

    if not swinging then
        char.PrimaryPart.CFrame = game.Workspace.Cylinder.CFrame * CFrame.new(0, 10, 0)
        weld.Part0 = game.Workspace.Cylinder
        weld.Part1 = char.PrimaryPart
        weld.Parent = char
        weld.C1 = CFrame.new(0, 2, 0)
        animTrack:Play()
        swinging = true
    else
        weld.Parent = game.ReplicatedStorage
        task.wait(0.1)
        animTrack:Stop()
        swinging = false
    end
end

game.ReplicatedStorage.SwingEvent.OnServerEvent:Connect(onSwingEvent)

Its because you keep resetting the “animTrack” Variable every time the function is called, meaning that its creating a new animation every time and then stopping that animation instead of the original animation.

Simple Summary

Basically your animTrack variable is getting reset
Easy fix is to only set the animTrack variable only if you aren’t swinging