How to make a reloading system?

Hello, so i got this script for my automatic gun (its a local script)

local gun = script.Parent

local gun_shot = game.ReplicatedStorage["Gun shot"]


local ReplicatedStorage = game:GetService('ReplicatedStorage')

local remoteEvent = ReplicatedStorage:WaitForChild('ShotEvent2')


local shooting = false

local equipped = false


local player = game.Players.LocalPlayer

local mouse = player:GetMouse()
 local reloading = false

local mouseConnection


gun.Equipped:Connect(function()



	equipped = true



	mouseConnection = mouse.Button1Down:Connect(function()
		if not shooting and not reloading and gun.Ammo.Value > 0 then
gun.Flash.BillboardGui.Enabled = true
		shooting = true
		gun.Flash.BillboardGui.Enabled = true
			gun.Flash.BillboardGui.Enabled = true
			remoteEvent:FireServer(gun.Handle.Position, gun.Handle.Orientation, mouse.Hit.p)
			gun.Flash.BillboardGui.Enabled = true
			gun_shot:Play()
			gun.Smoke.ParticleEmitter:Emit()
			gun.Flash.BillboardGui.Enabled = true
			gun.Ammo.Value -= 1
        mouse.Button1Up:Connect(function()
				gun.Flash.BillboardGui.Enabled = false
				shooting = false
			end)
		end
		wait(0.1)
	end)


gun.Unequipped:Connect(function()

	equipped = false

	mouseConnection:Disconnect()
end)
end)



userinputservice.InputBegan:Connect(function(input, gameProcessed)
	if not gameProcessed then
		if input.UserInputType == Enum.UserInputType.Keyboard then
			local keycode = input.KeyCode
			if keycode == Enum.KeyCode.R and not reloading then
				reloading = true
				wait(cooldown) -- Add a cooldown before allowing shooting again after reloading
				gun.Ammo.Value = 15
				player:FindFirstChild("ParticleEmitter").Parent = gun.Smoke
				player:FindFirstChild("BillboardGui").Parent = gun.Flash
				gun.Magazine.Value = gun.Magazine.Value - gun.Ammo.Value
				reloading = false
			end
		end
	end
end)

i’ve tried doing this:


elseif gun.Ammo.Value == 0 then
		game.ReplicatedStorage.clip_empty:Play()
        mouse.Button1Up:Connect(function()
				gun.Flash.BillboardGui.Enabled = false
				shooting = false
			end)
		end
		wait(0.1)
	end)


gun.Unequipped:Connect(function()

	equipped = false

	mouseConnection:Disconnect()
end)
end)

but it just doesn’t work. the gun shoots 1 bullet and stops working.