Reloading when the ammo reserve is under 30, causes the ammo reserve to go into negative values and causes addition to go wrong. This is weird way to explain this but I provided a gif on what the problem is.
https://gyazo.com/51c0ff014427922a4084763397b870bc
The ammo reserve is supposed to be subtracted by itself, and the subtracted ammo reserve would be added onto the current ammo count, instead it got subtracted by 30 (the ammo capacity) and it got added onto the current ammo count.
It is really hard to explain this, but maybe I’m just bad at math.
Here is my code:
function gun:Reload()
if self.Ammo < self.Capacity and not self.Aiming and not self.Shooting and not self.Leaning and not self.Reloading then
self.Reloading = true
if self.Ammo <= self.AmmoReserve then
local calculated = self.Capacity - self.Ammo
self.Ammo = self.Ammo + calculated
self.AmmoReserve = self.AmmoReserve - calculated
else
self.Reloading = false
end
end
self.Reloading = false
return self.Ammo
end
self.Capacity refers to the ammo capacity and it is 30.
Any suggestions on how I can remake this?