Hi so I am trying to make a reloading function, and since I am bad at math I do not know how to make this,
I have 2 values here
Ammo is how many bullets you have and magazine is how much ammo you have left to fire, and I have another value in a module script which is maxammo but I dont think thats important here.
Let’s say I have 15 bullets in my magazine but only 10 bullets in total ammo, and the max ammo is 30. How do I make the script know it has to substract the last 10 bullets instead of subtracting 15 bullets or 30?
Ok this is my opnion
While wait() do
if Ammo.Value ~= 0 then
magazine.Value = magazine.Value + 1
end
end
if the bullets player have didn’t have zero this script will work, also you can use this code to modify your script
Let’s say these variables have already been declared:
CurrentAmmo, MagazineCapacity, AvailableAmmo.
Your NeededAmmo would be MagazineCapacity - CurrentAmmo.
ReloadedAmmo = math.clamp(NeededAmmo, 0, AvailableAmmo) would be how much ammo is actually reloaded.
CurrentAmmo += ReloadedAmmo would be the new amount.
AvailableAmmo = TotalAmmo - ReloadedAmmo would be how much is left after reloading.
Here’s something that will work, this will get rid of the ammo from the magazine, put it in the total ammo then reload with the remaining total ammo.
script.Parent.Ammo.Value += script.Parent.Magazine.Value
script.Parent.Magazine.Value = 0
if script.Parent.Ammo.Value >= 30 then
script.Parent.Ammo.Value -= 30
script.Parent.Magazine.Value = 30
return
end
if script.Parent.Ammo.Value < 30 then
script.Parent.Magazine.Value = script.Parent.Ammo.Value
script.Parent.Ammo.Value = 0
return
end
This is an example of polling and something you should avoid. Use the proper events provided by Roblox.
Ammo.Changed:Connect(function()
if Ammo.Value ~= 0 then
magazine.Value += 1
end
end)