I need help with some random bug that just popped up in my gun system

So, for some reason when i unequip the tool while it is not full, you are able to reload the gun even when it is not equipped

code:

local UserInputService = game:GetService(“UserInputService”)
local RepStorage = game:GetService(“ReplicatedStorage”)

local idle = script.Parent.Anims.Idle
local reloadanim = script.Parent.Anims.Reload
local kickBack = script.Parent.Anims.Fire
local reloadTrack = nil
local idleTrack = nil
local kickTrack = nil

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local tool = script.Parent
local gui = tool.gui

local max_ammo = 10
local current_ammo = max_ammo
local firemode = “Semi” – Change this to “semi” or “auto” as needed

local reload_duration = 1
local cooldown_duration = 0.9
local cooldown = false

local firing = false
local reloading = false

– Recoil
local recoilShakerMod = require(RepStorage.CameraShaker)
local camera = workspace.CurrentCamera
local cameraShake = recoilShakerMod.new(Enum.RenderPriority.Camera.Value, function(shakeCFrame)
camera.CFrame = camera.CFrame * shakeCFrame
end)

cameraShake:Start()

function recoil()
cameraShake:Shake(recoilShakerMod.Presets.Bump)
end

local function reload()
if reloading or current_ammo == max_ammo then return end

reloading = true
reloadTrack:Play()
tool.Sounds.Reload:Play()
tool.gunmodel.Ammo.Transparency = 0
gui.gun_framethingy.ammo.Text = "Reloading..."

task.wait(tool.Sounds.Reload.TimeLength)
tool.gunmodel.Ammo.Transparency = 1
current_ammo = max_ammo
gui.gun_framethingy.ammo.Text = current_ammo.."/"..max_ammo
reloading = false

end

local function fire()
if current_ammo <= 0 then
reload()
return
end
if reloading then return end
if cooldown then return end

current_ammo -= 1
gui.gun_framethingy.ammo.Text = current_ammo.."/"..max_ammo

tool.fire:FireServer(tool.Handle.muzzle.WorldPosition, mouse.Hit.Position)
kickTrack:Play() -- Ensure the kick animation plays each time
recoil()

cooldown = true
task.wait(cooldown_duration)
cooldown = false

if current_ammo <= 0 then
	reload()
end

end

local function startFiring()
if reloading or cooldown then return end – Prevent firing if reloading or cooldown is active

firing = true

if firemode == "Semi" then
	fire()
elseif firemode == "Auto" then
	while firing and current_ammo > 0 do
		fire()
		task.wait(cooldown_duration) -- Control auto fire rate
	end
end

end

local function stopFiring()
firing = false
end

tool.Activated:Connect(startFiring)
tool.Deactivated:Connect(stopFiring)

UserInputService.InputBegan:Connect(function(input, processed)
if processed then return end

if input.KeyCode == Enum.KeyCode.R then
	reload()
end

end)

UserInputService.TouchStarted:Connect(function(touch, processed)
if processed then return end
startFiring()
end)

UserInputService.TouchEnded:Connect(function(touch, processed)
if processed then return end
stopFiring()
end)

tool.Equipped:Connect(function()
gui.Parent = player.PlayerGui
mouse.Icon = “rbxassetid://12829852445”
gui.gun_framethingy.ammo.Text = current_ammo…“/”…max_ammo
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild(“Humanoid”)
kickTrack = hum.Animator:LoadAnimation(kickBack)
reloadTrack = hum.Animator:LoadAnimation(reloadanim)
idleTrack = hum.Animator:LoadAnimation(idle)
idleTrack:Play()
end)

tool.Unequipped:Connect(function()
gui.Parent = tool
idleTrack:Stop()
mouse.Icon = “”
end)

sorry if the post is messy, i just dont use the fourm often

im tryna make a gun system like centaura

you need to stop reloading when the gun get unequipted , personally i use i repeat loop the wait like 0.1 sec and check if the a var is true then that means that it can continue if not it should stop with out resetting the ammo to full again and when the gun get equip it check if the ammo is 0 if so it reload