Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I'm not sure how to calculate how much ammo I should subtract?

So I have a gun, right. But the thing is, I want to create an ammo gui, and I want there to be a max ammo. The only poblem I have with making the max ammo thing I have is finding out how much ammo I subtract from the counter. Like you could do

maxAmmo = 270

maxAmmo = maxAmmo - 30 --//30 = amount of rounds in a mag

But then even if I had, let's say, 21 rounds in a mag, and only need to subtract 9 from the Max Ammo Counter, it would still subtract 30. You could also make it so that you can only reload when you have 0 ammo, which would look like this

maxAmmo = 270
ammo = 30

if ammo == 0 then
	maxAmmo = maxAmmo - 30
end

But that would be annoying, because if you wanted a full mag, you would have to waste the rounds in the mag to reload.

For another example, let's say I had 24 rounds in the magazine, and the Max Ammo Counter has 270 rounds left, I would only need to subtract 6 rounds from the counter, but I am unsure on how to calculate this.

Does anyone know how to calculate this? I've seen games like arsenal or Phantom Forces do it.

Thanks in advance, Zander.

8 comments

0

I can't visualize what your goal is. Please show examples... Why suddenly 9?

0

Bruh, do you mean add?

0

Does my last example give you a better understanding?

0

Feahren, If I only had 24 rounds in the mag, I only want to add 6 rounds to my magazine (Fill it to capacity), and subtract 6 rounds from the max ammo counter (270).

0

No, I don't understand what you mean by subtract, what equilibrium are you trying to reach?

0

So, like I said, I have 24 rounds in my gun magazine, then, I have 270 rounds in reserve. I want to change the amount of bullets in the magazine 30, while subtracting 6 rounds (the amount of rounds I put unto the mag) from 270 (the reserve amount).

0

Ah, that's much easier. Simple enough, let me whip that up for you.

0

Thank you so much!

1 answer

✓ Accepted answer
local userInputService = game:GetService("UserInputService")

local reserveAmmo = 270
local magCapacity = 30
local currentAmmo = magCapacity

userInputService.InputBegan:Connect(function(inputObject, gameProcessed)
	if (gameProcessed) then return end
	if (inputObject.KeyCode == Enum.KeyCode.R) then
		local bulletCompensation = (magCapacity - currentAmmo)
		currentAmmo = (currentAmmo + bulletCompensation)
		reserveAmmo = (reserveAmmo - bulletCompensation)
	end
end

2 comments

0

Holy moly. I cannot thank you enough!!!

0

No problem

This discussion is preserved as it was. New answers, comments, and votes are closed.