My current reloading logic is wrong and leading to problems. I am trying to make it so you gain ammo logically. This is very annoying.

For an example lets say the full magazine can hold 10 ammo. You have in total 100 ammo. You use up the magazine and reload. Your total ammo is now 90, however, lets say you have 0 magazine ammo but 3 ammo in total. You reload and only get 3 instead of 10 for the magazine ammo. I am trying to achieve that.

My code:

``````    if magAmmo.Value >= totalAmmo.Value then
magAmmo.Value += totalAmmo.Value
end

totalAmmo.Value -= math.min(totalAmmo.Value,math.abs(8-magAmmo.Value))

if magAmmo.Value < totalAmmo.Value then
local numbersNeededToEqual8 = 8-magAmmo.Value
magAmmo.Value += numbersNeededToEqual8
end

if magAmmo.Value > 8 then
magAmmo.Value = 8
end
``````

Appreciate any help.

Sorry for the late response but do you have any idea how to implement that into my code? I tried something like this,

``````    magAmmo.Value += math.clamp(totalAmmo.Value,0,8)
totalAmmo.Value -= magAmmo.Value

if magAmmo.Value > 8 then
magAmmo.Value = 8
end

if totalAmmo.Value < 0 then
totalAmmo.Value = 0
end
````````
``````local MaxClip = 8
local MaxAmmo = 100

local CurrentClip = MaxClip
local CurrentAmmo = MaxAmmo

if CurrentClip >= MaxClip then return warn'You don\'t need to reload, your clip is full' end
if CurrentAmmo <= 0 then return warn'You don\'t have any ammo to load' end

local FillAmount = math.min(MaxClip - CurrentClip, CurrentAmmo)
print(string.format("Refilling %d bullets, %d ammo left", FillAmount, CurrentAmmo - FillAmount))
CurrentClip += FillAmount
CurrentAmmo -= FillAmount
print(string.format("After reload: %d / %d", CurrentClip, CurrentAmmo))
end

CurrentClip = 5
Reload() --> Refilling 3 bullets, 97 ammo left
--> After reload: 8 / 97

CurrentAmmo = 0
CurrentClip = 8

CurrentClip = 7