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)
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
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)