Gun doesnt stop shooting after ammo reached 0

This script allows for a gun to shoot bullets at enemies but when the ammo reaches 0 it continues to shoot and displays the ammo value as -(number).
I tried changing some things like shooting = false.
I would like to make the gun stop shooting when the ammo value reaches 0

local gun = script.Parent

local shoot = gun.Handle.Shoot
local reload = gun.Handle.Reload
local empty = gun.Handle.Empty
local reloadAnim = gun.Handle.ReloadAnim
local player = game.Players.LocalPlayer
local clipSize = gun:WaitForChild('Ammo').Value
local ammo = gun:WaitForChild('Ammo')

local userInput = game:GetService('UserInputService')

local shootEvent = script.Parent.ShootEvent



local shooting = false

local equipped = false



local player = game.Players.LocalPlayer

local mouse = player:GetMouse()



local mouseConnection



gun.Equipped:Connect(function()

	player.PlayerGui.CameraGui.GameInfo.AmountAmmo.Visible = true
	player.PlayerGui.CameraGui.GameInfo.AmountAmmo.Text = ''.. tostring(ammo.Value)..'/'.. tostring(gun.MaxAmmo.Value)

	equipped = true



	mouse.Icon = 'rbxassetid://117431027'



	mouseConnection = mouse.Button1Down:Connect(function()
		if gun.Ammo.Value > 0 then
			shooting = true

			while shooting and equipped do

				shootEvent:FireServer(gun.Handle.Position, gun.Handle.Orientation, mouse.Hit.p)

				shoot:Play()
				gun.Ammo.Value = gun.Ammo.Value - 1

				mouse.Button1Up:Connect(function()

					shooting = false

				end)

				wait(0.1)
		

			end
		else
			empty:Play()
		end
	end)
	
end)

gun.Unequipped:Connect(function()
	player.PlayerGui.CameraGui.GameInfo.AmountAmmo.Visible = false
end)

userInput.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 then
				if gun.Ammo.Value < clipSize and gun.MaxAmmo.Value > 0 then
				    reload:Play()
					reload.Ended:Wait()
					if gun.MaxAmmo.Value - (clipSize - gun.Ammo.Value) >= 0 then
						gun.MaxAmmo.Value = gun.MaxAmmo.Value - (clipSize - gun.Ammo.Value)
						gun.Ammo.Value = clipSize
					else
						gun.Ammo.Value = gun.Ammo.Value + gun.MaxAmmo.Value
						gun.MaxAmmo.Value = 0
						player.PlayerGui.CameraGui.GameInfo.AmountAmmo.Text = ''.. tostring(ammo.Value)..'/'.. tostring(gun.MaxAmmo.Value)
					end
					
				end	
			end
		end
	end
end)

ammo.Changed:Connect(function()
	player.PlayerGui.CameraGui.GameInfo.AmountAmmo.Text = ''.. tostring(ammo.Value)..'/'.. tostring(gun.MaxAmmo.Value)
end)

gun.Unequipped:Connect(function()

	equipped = false

	mouseConnection:Disconnect()

end)
1 Like

you should add a
if ammo > 0 then
before the gun shoot

1 Like

You can modify this line to include this:

while shooting and equipped and game.Ammo.Value > 0 do

And this function, since it is never disconnected and doesn’t need to be disconnected, should be moved outside of everything.

1 Like

Thanks guys! If there were an option to choose multiple posts as solutions, I would do that. But I can only do one post