I was working on a very simple weapon HUD and I ran into a problem where my reserve ammo counter kept going into the negatives if the player reloaded early. I quickly found a solution, which was making it so that the player can’t reload early, but I realized that this would probably get infuriating in-game. I’ve tried several things like checking if the Reserve <= 30 then to make the ammo’s value equal to the reserve’s value, and make the new reserve value 0. This is how I tried this:
if Reserve <= 30 then
Ammo = Reserve
AmmoCounter.Text = Reserve
ReserveAmmo.Text = 0
end
But this didn’t really seem to work, but at the same time I wasn’t sure if I was using it correctly.
Everything in the script is predefined, and the Ammo and Reserve values are stored in the script itself, not in an outside intvalue.
Here is the portion of my code that handles the ammocounter text.
UserInputService.InputBegan:Connect(function(Input, gameFinished)
if (not gameFinished) then
if Input.KeyCode == Enum.KeyCode.R then
character.Humanoid:LoadAnimation(script.Parent.reload):Play()
if InfAmmo == false then
if Reserve ~= 0 then
if Reloading == false then
if Ammo ~= 30 then
Reloading = true
wait(ReloadingTime)
Reloading = false
Reserve = Reserve - (30 - Ammo)
Ammo = MaxAmmo
ReserveAmmo.Text = Reserve
AmmoCounter.Text = Ammo
end
end
end
end
end
end
end)
while wait() do
if buttononedown then
while wait() do
if Reloading == false then
if Ammo ~= 0 then
Ammo = Ammo - 1
AmmoCounter.Text = Ammo
ReserveAmmo.Text = Reserve
script.Parent.rayCast:FireServer(script.Parent.Muzzle.Position, mouse.Hit.Position, script.Parent.Eject)
shake()
script.Parent.Flash.Light.Enabled = true
RunService.Stepped:Wait()
script.Parent.Flash.Light.Enabled = false
else
if InfAmmo == false then
if Reserve ~= 0 then
if Reloading == false then
Reloading = true
wait(ReloadingTime)
Reloading = false
Reserve = Reserve - (30-Ammo)
Ammo = MaxAmmo
ReserveAmmo.Text = Reserve
AmmoCounter.Text = Ammo
end
end
end
end
else
end
wait(0.03)
if not buttononedown then
break
end
end
end
end
end)
Where can I impliment what I need?