Alright so im making a fps game. everything works but i realized that the player’s can shoot forever. so i was wondering how to make a reloading system. but since i don’t know, im trying to figure out how to make it but nothing is helping me.
Just have a variable named ammo
that keeps track of current ammo, and before firing the weapon, check if ammo > 0 and once the weapon is fired reduce the ammo by 1 (ammo -= 1). Now for reloading, assuming you have a function called reload
just set the ammo back to 30 after playing the reload animation.
uhhhhhh… you mean like do i just do this?
local ammo = 30
and then do the rest?
You can make reload function like this :
local function ReloadFunction(Player)
if Reloading or Ammo.Value == MaxAmmo then
return
end
Reloading = true
AmmoCloneUI.AmmoText.Text = "Reloading..."
ReloadSound:Play()
ReloadSound.Ended:Wait()
Ammo.Value = MaxAmmo
task.wait(ReloadTime.Value)
Reloading = false
end
This is as example of reload system for me. The rest are yours.
Depends on the Reloading System you are trying to make, which can be a bit complicated.
Here are the Basics, lets say you have 18 bullets on a pistol you have, you are able to store 1 in the chamber with 18 in the magazine, in total, 19 bullets can be held by the weapon, if you have only 9 magazines (18 multipied by 9), which in this case would be 162, thats the amount of ammo you have in reserve, when the weapon is fired, you obviously have to subtract the amount of ammo you’re using, with in this case 18 - 19 bullets.
Depending on your System, It may allow you to reload while you still have a loaded weapon, or you can only reload when the weapon is completely empty, for this you have to keep track of the total amount of ammo you can carry (18), and then reset with that amount , but you have bullets in reservation, you must take away from that amount, and put it into the weapon.
When doing this, you have account for low, or no ammo left, if you have 5 bullets in the current magazine, and only 1 left in reserve, which you have to add that amount with the current amount you have.
You can do something like this for that kind of system:
local TotalCapacity = 18 -- The Total amount of Ammo we can Hold
local CurrentAmount = 1 -- The Amount of Ammo we have in out Gun
local Reserved = 6 -- the Amount we have left in Reserve
local function Reload()
local AmmoNeeded = TotalCapacity - CurrentAmount
-- this will give us the amount of bullets we used so far
local AmmoTaken = Reserved - AmmoNeeded
-- This will give us the Amount to Subtract by
-- it is essentially the amount of ammo we are taking from the reserved
-- amount
if AmmoTaken >= 0 then
-- this is basically check if we are able to add a full mag
-- into our weapon, as the amount in Reserve changes
Reserved -= AmmoTaken
-- we take away from our reserved to fill the mag
CurrentAmount = TotalCapacity -- gives a full mag as we have enough
else
-- if there is not enough ammo to completely fill the mag
CurrentAmount += Reserved -- adds the remaining amount to the current
Reserved = 0 -- left with 0 ammo in reserve
end
end
print(`{CurrentAmount}/{Reserved}`) -- what we had before the reload
Reload()
warn(`{CurrentAmount}/{Reserved}`) -- what we had after
(Sorry this took forever, was making sure I had everything correct)