Need tips for gun system

I’m trying to make a gun system, this is how it’s looking right now:

Main module script:

local framework = setmetatable({}, {})
framework.__index = framework

function framework.new(tool, config)
	local self = setmetatable({}, framework)
	
	self.Instance = tool
	self.Config = require(config)
	
	
	tool.Equipped:Connect(function()
		print(self.Config.name .. " equipped")
	end)
	
	return self
end


return framework

local script in each gun:

local framework = require(game:GetService('ReplicatedStorage').GunFramework)
local gun = framework.new(script.Parent, game:GetService('ReplicatedStorage')["Guns Data"]["G17"])

Is there a better way to do this? The scripts in each gun/tool are all basically the exact same which leaves me just copy and pasting the same code and making minimal changes, aside from the name of the gun. I’m looking for a different way I can be doing this

Take a look at CollectionService. Assign a weapon tag to all weapons and then run the code for every weapon by iterating over game:GetService("CollectionService"):GetTagged("weapon").

1 Like

Any idea why this isn’t working?

local CS = game:GetService('CollectionService')
local framework = require(game:GetService('ReplicatedStorage').GunFramework)

for _, Gun in CS:GetTagged("Gun") do
	framework.new(Gun, game:GetService('ReplicatedStorage')["Guns Data"][Gun.Name])
end

I put the gun tag on the tools, and when I print Gun.Name, it prints the names of all the tools but when I do framework.new it doesn’t do anything