Saving ammo in the mag

hello devform it’s me again, in my tactical attachment system the player has the ability to switch mags. the problem that i am facing is that when the mag is switched the ammo value resets

video if the problem:

here is the script that has the mags value:

local Values = {
	ammoValues = {
		maxAmmo = 23,
		magCapacity = 40
	}
}



return Values

here is the line of code that handles the ammo change:

if #self.viewModel.weapons.mag:GetChildren() == 0 then
		self.ammo[self.wepName] = 0
		self.maxAmmo[self.wepName] = 0
		self.wepGui.ammo.Text = 'Ammo:' .. tostring(self.ammo[self.wepName]) .. ' / ' .. tostring(self.maxAmmo[self.wepName])
	else
		local values = require(self.viewModel.weapons.mag.values)
		self.ammo[self.wepName] = values.ammoValues.magCapacity
		self.maxAmmo[self.wepName] = values.ammoValues.maxAmmo
	end
1 Like

Sounds like the simplest solution is adding another key to the ammoValues table called ammo which represents the current ammo in that magazine. When loading that magazine, read from that key instead of the magCapacity key.

self.ammo[self.wepName] = values.ammoValues.ammo

Of course, when switching away from that magazine, you’ll also need to set that value. Dunno where that happens in your code, but it would look like the inverse operation:

values.ammoValues.ammo = self.ammo[self.wepName]
2 Likes

would the key for the ammo be a table?

A number would probably make more sense, but it’s your game so you might have some other data to plug in. Personally, I would use Attributes on the Tool instead of a ModuleScript altogether.

1 Like

Thanks bro now i just gotta think of a way to implement it

1 Like