Ammo system not working properly

Hello Developers,

I’m trying to make an ammo system for a machine gun and it deducts ammo just fine the first time around, but the second time around it will stay on 199 or 198 for about 20-30 bullets, then go back to normal.

Code:

local tool = script.Parent.Parent
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local fireEvent = tool:WaitForChild("RemoteEvents"):WaitForChild("FireEvent")

local ammo = tool:WaitForChild("Ammo").Value
local hasAmmo = true

local userInputService = game:GetService("UserInputService")
local runService = game:GetService("RunService")
local camera = workspace.CurrentCamera

local isEquipped = false
local cooldown = false

tool.Equipped:Connect(function()
	isEquipped = true
end)

tool.Unequipped:Connect(function()
	isEquipped = false
end)

runService.Stepped:Connect(function()
	if cooldown == false  and isEquipped == true and hasAmmo == true then 
		
		if userInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1) then
			local mousePosition = userInputService:GetMouseLocation()
			local ray = camera:ViewportPointToRay(mousePosition.X, mousePosition.Y)
			
			fireEvent:FireServer(ray)
			
			cooldown = true
			wait(0.03)
			ammo = ammo - 1
			if ammo == 0 then
				hasAmmo = false
			end
			print(ammo)
			cooldown = false
		end
		
	end
	
	if hasAmmo == false then
		wait(3)
		ammo = 200
		wait(.5)
		hasAmmo = true
	end
	
end)

First time:
Screen Shot 2023-06-16 at 2.28.09 PM

Second:
Screen Shot 2023-06-16 at 2.28.21 PM

Please help if you can.

1 Like

if hasAmmo == false then is the problem, it runs alot of times before hasAmmo gets set to true so your gun basicly reloads those 20-30 times

1 Like

How would I stop it from running so much?

1 Like

change

runService.Stepped:Connect(function()
	if cooldown == false  and isEquipped == true and hasAmmo == true then 
		
		if userInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1) then
			local mousePosition = userInputService:GetMouseLocation()
			local ray = camera:ViewportPointToRay(mousePosition.X, mousePosition.Y)
			
			fireEvent:FireServer(ray)
			
			cooldown = true
			wait(0.03)
			ammo = ammo - 1
			if ammo == 0 then
				hasAmmo = false
			end
			print(ammo)
			cooldown = false
		end
		
	end
	
	if hasAmmo == false then
		wait(3)
		ammo = 200
		wait(.5)
		hasAmmo = true
	end
	
end)

to

while runService.Stepped:Wait() do
	if cooldown == false  and isEquipped == true and hasAmmo == true then 
		
		if userInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1) then
			local mousePosition = userInputService:GetMouseLocation()
			local ray = camera:ViewportPointToRay(mousePosition.X, mousePosition.Y)
			
			fireEvent:FireServer(ray)
			
			cooldown = true
			wait(0.03)
			ammo = ammo - 1
			if ammo == 0 then
				hasAmmo = false
			end
			print(ammo)
			cooldown = false
		end
		
	end
	
	if hasAmmo == false then
		wait(3)
		ammo = 200
		wait(.5)
		hasAmmo = true
	end
	
end

so it runs in a loop

1 Like

The section of your code that is intended to execute once when your ammunition is depleted is currently running multiple times. To address this issue, I suggest introducing a boolean variable named “isReloading.”

local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local fireEvent = tool:WaitForChild("RemoteEvents"):WaitForChild("FireEvent")

local ammo = tool:WaitForChild("Ammo").Value
local hasAmmo = true

local userInputService = game:GetService("UserInputService")
local runService = game:GetService("RunService")
local camera = workspace.CurrentCamera

local isEquipped = false
local cooldown = false
local isReloading = false

tool.Equipped:Connect(function()
	isEquipped = true
end)

tool.Unequipped:Connect(function()
	isEquipped = false
end)

runService.Stepped:Connect(function()
	if cooldown == false  and isEquipped == true and hasAmmo == true then 

		if userInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1) then
			local mousePosition = userInputService:GetMouseLocation()
			local ray = camera:ViewportPointToRay(mousePosition.X, mousePosition.Y)

			fireEvent:FireServer(ray)

			cooldown = true
			wait(0.03)
			ammo = ammo - 1
			if ammo == 0 then
				hasAmmo = false
			end
			print(ammo)
			cooldown = false
		end

	end

	if hasAmmo == false and isReloading == false then
		isReloading = true
		wait(3)
		ammo = 200
		wait(.5)
		hasAmmo = true
		isReloading = false
	end

end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.