Cancel shooting sequence at any point on unequip?

I am not at all sure how i will manage this as i could not think of much methods and any methods i did try didn’t work.

so what i want to achieve is when the player unequips anytime during the shooting sequence of the gun such as aiming and shooting the rest of the sequence will stop and if it’s during shooting there is a cooldown.

script:

local Replicated = game:FindFirstChild("ReplicatedStorage")
local Players = game:GetService("Players")
local Debris = game:GetService("Debris")

local FrameWork = Replicated.WeaponFramework
--local SE = FrameWork.Events.Shoot
local RNG = Random.new()
local TAU = math.pi * 2

local Tool = script.Parent
local Handle = Tool.Handle
local Settings = require(Tool.SettingsModule)
local Mesh = Tool.Mesh
local BarrelAttachment = Mesh:WaitForChild("BarrelAttachment")

local Aiming = false
local Tracer = Replicated.Tracer
local spread = Settings["Spread"]
local Range = Settings["Range"]
local BulletsPerShot = Settings["BulletsPerShot"]
local BurstAmount = Settings["BurstAmount"]
local RecoveryTime = Settings["RecoveryTime"]

local Sounds = Handle.Sounds
local ShootSound = Sounds.Shoot
local Equip = Sounds.Equip

local Flash = BarrelAttachment.Flash
local Smoke = BarrelAttachment.Smoke
local SE = Tool:FindFirstChild("Shoot")

Tool.Equipped:Connect(function()
	Equip:Play()
end)



local rng_v = Random.new()
function RandomVectorOffset(v, maxAngle) --returns uniformly-distributed random unit vector no more than maxAngle radians away from v
	return (CFrame.new(Vector3.new(), v)*CFrame.Angles(0, 0, rng_v:NextNumber(0, 2*math.pi))*CFrame.Angles(math.acos(rng_v:NextNumber(math.cos(maxAngle), 1)), 0, 0)).LookVector
end

local function GenerateSpread(InitialDirection: Vector3): {Vector3}
	local SpreadDirections = {}
	for i = 0, BulletsPerShot - 1 do
		table.insert(SpreadDirections, RandomVectorOffset(InitialDirection, math.rad(spread)))
	end
	return SpreadDirections
end

SE.OnServerEvent:Connect(function(Player, Character, Hit)
Hit = Hit.PrimaryPart
print("recieved")
	local tracerClones = {}  -- Create a table to store tracer clones
	local OriginalSpeed = Character:FindFirstChild("Humanoid").WalkSpeed
	local Gryo = nil
	local Troll = Character.PrimaryPart:FindFirstChild("Troll")
	Character:FindFirstChild("Humanoid").AutoRotate = false

	if not Troll then
		Gryo = Tool.Duplicates.BodyGyro:Clone()
		Gryo.Name = "Troll"
		Gryo.Parent = Character.PrimaryPart
		Gryo.MaxTorque = Vector3.new(0, 0, 0)
		Gryo.P = 5000
		Gryo.D = 250
	end

	Aiming = true

	task.spawn(function()
		if Aiming then
			repeat
				task.wait()
				Gryo.CFrame = CFrame.lookAt(Character.PrimaryPart.Position, Hit.Position)
				--print("ROTATE")
			until not Aiming
		end
	end)
	Character:FindFirstChild("Humanoid").WalkSpeed = Settings["Slowdown"]
	Gryo.MaxTorque = Vector3.new(0, 2000, 0)
	task.wait(Settings["AimTime"])

	for j = 0, BurstAmount - 1 do
		task.wait(Settings["BurstDelay"])
		ShootSound:Play()

		local rp = RaycastParams.new()
		rp.IgnoreWater = true
		rp.FilterType = Enum.RaycastFilterType.Blacklist
		rp.FilterDescendantsInstances = {Tool, Character}

					local BarrelAtt = BarrelAttachment.WorldPosition
					local InitialDirection = (Hit.Position - BarrelAtt).Unit
					local SpreadDirections = GenerateSpread(InitialDirection, BulletsPerShot, spread)
					local tracers = {}  -- Table to store tracers for destruction later

					for _, UnitVector in pairs(SpreadDirections) do
			Flash:Emit(100)
			local TracerClone = Tracer:Clone()
			TracerClone.Parent = workspace
			TracerClone.START.WorldPosition = BarrelAtt
			TracerClone.END.WorldPosition = BarrelAtt + (UnitVector * Range)
			table.insert(tracers, TracerClone)  -- Store tracers for later destruction
			local dir = TracerClone.END.WorldPosition - BarrelAtt
			local destination = BarrelAtt + dir

			local ray = workspace:Raycast(BarrelAtt, UnitVector * Range, rp)
		--	local ray = workspace:Raycast(BarrelAtt, BarrelAtt + (UnitVector * Range), rp)
			if ray then
			local HitLocation = ray.Position
				TracerClone.END.WorldPosition = HitLocation
				hitPart = Instance.new("Part")
				hitPart.Size = Vector3.new(0.1,0.1,0.1)
				hitPart.Position = ray.Position
				hitPart.Anchored = true
				hitPart.CanCollide = false
				hitPart.Transparency = 0.5
				hitPart.BrickColor = BrickColor.new("Bright red")
				hitPart.Parent = workspace -- Change parent if needed
			Tracer.END.WorldPosition = HitLocation



				local RayHit = ray.Instance
				local Hum = RayHit.Parent:FindFirstChild("Humanoid")
				if Hum then
					Hum:TakeDamage(Settings["Damage"])
					print("Hit!")
				else
					print("Miss!")
				end
			end
		end


		task.wait(0.1)
		--hitPart:Destroy()
			for _, tracer in pairs(tracers) do
				tracer:Destroy()  -- Destroy tracers after a delay
		end
	end

	-- Destroy all tracer clones after all iterations
	for _, tracer in ipairs(tracerClones) do
		task.wait(0.1)
		tracer:Destroy()
	end
	if Gryo then
	Gryo:Destroy()
	end
	Character:FindFirstChild("Humanoid").AutoRotate = true
	Character:FindFirstChild("Humanoid").WalkSpeed = OriginalSpeed
Aiming = false
--Smoke.Enabled = true
Tool.Enabled = false
task.wait(RecoveryTime)
--Smoke.Enabled = false
Tool.Enabled = true
end)


lmk if i need to provide the local script aswell!