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 Reload = script.Parent.Handle.Load
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)
game.Debris:AddItem(rayPart, 0.02)
local reloading = script.Parent.Parent.Humanoid:LoadAnimation(script.Reload)
reloading:Play()
wait(cooldown)
Reload:Play()
else
script.Parent.Handle.Miss:Play()
stinger.Sparks.Enabled = true
wait(.1)
stinger.Sparks.Enabled = false
end
game.Debris:AddItem(rayPart, 0.02)
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.

1 comment
I moved it to the start of the function and it still only counts 11