Handling GunAmmo

What is the best way to handle ammo from the server for a player.
image
This is my current system for my M4A1.

1 Like

You should store them inside a module script so the server value doesnt replicate to the client. This way now you can make the ammo value go lower on the client everytime they shoot so it seems instant and responsive, meanwhile the server gets through a remote the shootong request and lowers the ammo counter.

At the moment I have 2 vars on the server.

local CurrentAmmo = 50 -- // Ammo user has for this weapon
local MaxAmmo = 50 -- // MaxAmmo user can have for this weapon
local FireEvent = ... -- // Event triggered when firing 
local ReplicateAmmoEvent = ... -- // Event for replicating ammo to client
local ReloadEvent = ... -- // Event for reloading

FireEvent.OnServerEvent:Connect(function(_client: Player, Pos: Vector3)
	-- sanity checks
        if not (CurrentAmmo <= 0) then
              return
        end
        -- // fire gun 
        Fire(Pos) -- // do fire
        CurrentAmmo -= 1
        ReplicateAmmoEvent:FireClient(_client,CurrentAmmo,MaxoAmmo) -- // Changes Ammo GUI on client.

end)

ReloadEvent.OnServerEvent:Connect(function(_client: Player)
       CanFire = false
       CurrentAmmo = 50
       ReplicateAmmoEvent:FireClient(_client,CurrentAmmo,MaxAmmo) -- // Changes Ammo GUI on client.
       task.wait(2)
       CanFire = true
end)

NumberValues. Only use the value for gui displaying purposes (client-wise).

Maybe?
But constantly updating a number-value every second for a very high RPM gun couldent be good for performance?

Also why would I use Number-Values?
It seems like I would need to setup a loop on the client to constantly update the text to the number-value and this also sounds very performance tanking.
Right now I just have a remote that I call with the CurrentAmmo and MaxAmmo values and send it off to the client.

Why loop? Why can’t you just

local Ammo = script.Parent.Ammo
Ammo.Changed:Connect(function(Val)
   AmmoText.Text = Val .."/".. script.Parent.MaxAmmo.Value
end)
1 Like

Forgot .Changed exists!
:rofl:
So I should migrate to NumberValues for replicating Ammo?

Yes and remember only use the value for gui displaying. I use the same system for my weapons too.

1 Like

I mean dont you have a shoot function for your gun, cant you just update the value whenever you fire?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.