Gun Framework - All players share ammo

Hello All,

I’ve been trying to set up my own weapon framework (with fastcast) for guns. Everything works as I want it to when playing solo.

When I try it out with 2 or more players, for some reason the ammo is shared for all players. So when one players shoots a bullet, the other players have 1 less bullet left in their ammo.

No errors or anything. I think I am messing up with my methods.

Framework ModuleScript:

local FastCast = require(game.ServerScriptService.FastCastRedux)

local Caster = FastCast.new()

local CastParams = RaycastParams.new()
CastParams.IgnoreWater = true
FastCast.VisualizeCasts = true

local Behavior = FastCast.newBehavior()
Behavior.RaycastParams = CastParams
Behavior.AutoIgnoreContainer = false

local GunFramework = {}

GunFramework.__index = GunFramework

function GunFramework.new(Config) -- I feel like I'm doing something wrong here
	local newGun = setmetatable({}, GunFramework)
	
	newGun.Class = Config.Class
	newGun.Damage = Config.Damage
	newGun.AmmoInClip = Config.AmmoInClip
	newGun.MaxAmmoInClip = Config.MaxAmmoInClip
	newGun.Range = Config.Range
	newGun.ReloadTime = Config.ReloadTime
	newGun.TimeBetweenShots = Config.TimeBetweenShots
	newGun.TotalAmmo = Config.TotalAmmo

	newGun.Reloading = false
	newGun.Shooting = false
	
	
	function GunFramework:GunFire(origin, mousePosition, player)
		CastParams.FilterDescendantsInstances = {player.Character}
		if self.AmmoInClip > 0 and not self.Reloading then
			self.Shooting = true

			self.AmmoInClip -= 1

			local direction = (mousePosition - origin).Unit


			Caster:Fire(origin, direction, 1000, Behavior)
		else

			return false

		end

		self.Shooting = false

		return true


	end


	function GunFramework:Reload()

		if not self.Shooting and not self.Reloading and self.TotalAmmo > 0 then

			self.Reloading = true

			self.TotalAmmo -= self.MaxAmmoInClip - self.AmmoInClip

			self.AmmoInClip += self. MaxAmmoInClip - self.AmmoInClip

			print(self.TotalAmmo)

			wait(self.ReloadTime)
		end

		self.Reloading = false

	end
	
	
	
	
	
	return newGun
end

Server Script:

local FireEvent = game.ReplicatedStorage.Events.Fire
local ReloadEvent = game.ReplicatedStorage.Events.Reload
local Framework = require(game.ServerScriptService.GunFramework)
local GunConfig = {
	["FiveSeven"] = {
		["Name"] = "FiveSeven",
		["Class"] = "Pistol",
		["Damage"] = 25,
		["MaxAmmoInClip"] = 10,
		["Range"] = 75,
		["ReloadTime"] = 2,
		["TimeBetweenShots"] = 0.1,
		["TotalAmmo"] = 40,
		["AmmoInClip"] = 10,
		["Reloading"] = false,
		["Shooting"] = false
	}
}

local player = script.Parent.Parent.Parent
local tool = script.Parent

local Gun = Framework.new(GunConfig.FiveSeven)

FireEvent.OnServerInvoke = function(player, mousePosition, origin, tool) 

	local Fire = Gun:GunFire(origin, mousePosition, player)

	if Fire then
		return true
	else
		return false
	end

end

Thanks,

Ultan

I think you’re just overwriting the same table with every player. You need to make a seperate table for each player.
local FireEvent = game.ReplicatedStorage.Events.Fire
local ReloadEvent = game.ReplicatedStorage.Events.Reload
local Framework = require(game.ServerScriptService.GunFramework)
local GunConfig = {
[“FiveSeven”] = {
[“Name”] = “FiveSeven”,
[“Class”] = “Pistol”,
[“Damage”] = 25,
[“MaxAmmoInClip”] = 10,
[“Range”] = 75,
[“ReloadTime”] = 2,
[“TimeBetweenShots”] = 0.1,
[“TotalAmmo”] = 40,
[“AmmoInClip”] = 10,
[“Reloading”] = false,
[“Shooting”] = false
}
}

local player = script.Parent.Parent.Parent
local tool = script.Parent

local Gun = Framework.new(GunConfig.FiveSeven)

FireEvent.OnServerInvoke = function(player, mousePosition, origin, tool)

local Fire = Gun:GunFire(origin, mousePosition, player)

if Fire then
    return true
else
    return false
end

end