0

# Weapon Ammo counter not working?

Edited 20 days ago

Hello, Ive been working on a custom weapon for my game and have come across this peculiar issue. I had previous success counting down the ammunition by using a separate localscript from the raycasting/main one, but it fails to disable the raycasting script for some reason so I am trying to put the counter in the raycasting script instead.

When printing "Ammo", it always shows up as 12, which means its not updating the value for some reason?

local ammo = 12
local cooldown = 1.5

tool.Equipped:connect(function(mouse)
user = tool.Parent
mouse.Button1Down:connect(function()
if ammo >= 1 then
print(ammo)
local ray = Ray.new(tool.Barrel.CFrame.p, (mouse.Hit.p - tool.Barrel.CFrame.p).unit*300)
local hit, position = game.Workspace:FindPartOnRay(ray, user)
local distance = (position - tool.Barrel.CFrame.p).magnitude
local rayPart = Instance.new("Part", user)
rayPart.Name          = "RayPart"
rayPart.BrickColor    = BrickColor.new("Neon orange")
rayPart.Material      = Enum.Material.Neon
rayPart.Transparency  = 0
rayPart.Anchored      = true
rayPart.CanCollide    = false
rayPart.TopSurface    = Enum.SurfaceType.Smooth
rayPart.BottomSurface = Enum.SurfaceType.Smooth
rayPart.formFactor    = Enum.FormFactor.Custom
rayPart.Size          = Vector3.new(0.2, 0.2, distance)
rayPart.CFrame        = CFrame.new(position, tool.Barrel.CFrame.p) * CFrame.new(0, 0, -distance/2)

local SpawnPos = script.Parent.Parent.PrimaryPart
local Fire = script.Parent.Handle.Fire
local stinger = script.Parent.Stinger
local humanoid = hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid")
script.Disabled = true

if humanoid then
ammo = (ammo - 1)
tool.Name = ammo
Fire:Play()
stinger.Transparency = 1
Remote:FireServer(humanoid, SpawnPos)
wait(cooldown)
else
script.Parent.Handle.Miss:Play()
stinger.Sparks.Enabled = true
wait(.1)
stinger.Sparks.Enabled = false
end
script.Parent.Stinger.Transparency = 0
script.Disabled = false
end
end)
end)


Im not sure what Im doing wrong. I also only want it to count down if it hits a humanoid and deals damage, which is why the equation is where it is.

0

You are only removing the ammo when you hit a humanoid

Move the line of code

if humanoid then
ammo = (ammo - 1) -- Remove this line


to

if ammo >= 1 then
print(ammo)
ammo -= 1

0
I moved it to the start of the function and it still only counts 11 krankycaat 11 — 19d
0

I found the solution. Disabling the script after every shot caused the Ammo value to reset to 12. I fixed this by implementing a debounce.

local ammo = 12
local cooldown = 1.5
local debounce = false

tool.Equipped:connect(function(mouse)
user = tool.Parent
mouse.Button1Down:connect(function()
if ammo >= 1 and not debounce then
debounce = true