How do I get the number of ammunition from the backpack?


I’m building a survival game and my game is similar to Dayz. I want game guns to take their ammunition from the Player’s backpack, just like Dayz and Battle Royale games.

But the problem
My problem is that I do not know how to take the number of ammunition from the backpack and give it to the gun
And when he used the ammunition, the ammunition should be removed from the backpack
Or when the player drops ammunition from the backpack, the ammunition of the gun decreases
Just like the Battle Royale and Dayz games

I searched completely on YouTube and even on devforum roblox but there was no tutorial to help me (maybe I did not find it right)
Or I even wrote code, but when the player was dropping ammunition, the number of ammunition from the weapon did not decrease, or such problems
I have been looking for this answer for a week and I have not found anything and devforum is the last place I could go.

I use any kind of answer
Tell me if you need anything
Thanks for all the help

1 Like

To decrease a value you do variable = variable - 69
To increase a value you do variable = variable + 69

If you need more help, just reply to this and I’ll try my best to help! :slight_smile:

1 Like

I know this
You must have seen in Battle Royale games that when you pick up ammunition, it comes in your backpack and when you use it, it is cleared.
I want such a system

local player = game:GetService("Players").LocalPlayer
local TextButton = player.WaitForChild("PlayerGui"):WaitForChild("AmmoGui"):WaitForChild("AmmoText")
local Ammo = Instance.New("IntValue")

Ammo.Parent = player
Ammo.Name = Ammo
Ammo.Value = 0

TextButton.Text = Ammo.Value

I hope this helps, if not please tell me.

1 Like

In fact, the system I am going to build
It works in such a way that the gun always (or occasionally) monitors the player’s backpack so that if he has ammunition, the gun will start firing and show the number of ammunitions.

And when the gun runs out of ammunition, the ammunition in the backpack should be cleared
Or when the player throws ammunition out of his backpack, the ammunition of the weapon also decreases

And I do not know how to build this system

Just add Int values to the gun or player and remove a number when you reload or drop ammo.

1 Like

How do I reduce the number of ammunition when it drops? :thinking:

Number of Total Ammo - Number of Ammo Dropped

You subtract the numbers.

1 Like

Um, I better say
How do I know if the player has dropped or deleted the ammunition?

You could make it activate whenever the player clicks their mouse as that’s the only way I know of in games when ammo would decrease.