How would I call this from a module script?

I’m still pretty new to Object Oriented Programming and I’m a tad confused on how I would call the “secondary” part of my module script in a server script.

Example of code:

local Primaries = {}
Primaries.__index = Primaries

function Primaries.new(name, damage, ammo, auto)
	local newGun = {}
	setmetatable(newGun, Primaries)

	newGun.Name = name
	newGun.Damage = damage
	newGun.Auto = auto
    newGun.Ammo = ammo

	return newGun
end

local Secondaries = {}
Secondaries.__index = Secondaries

function Secondaries.new(name, damage, ammo)
	local newSecondary = Primaries.new(name, damage, ammo)
	setmetatable(newSecondary, Secondaries)
	
	return newSecondary
end

Then the server script would look something like this:

local Handler = require(script.WeaponHandler)

local Rifle = Handler.new()

which will call primaries but not secondaries. Any help? Thanks!

1 Like

Why not just have 2 tables within the existing module?

local Module = {}
Module.Primaries = {}
Module.Secondaries = {}

return Module
2 Likes

Well how would I go about creating the values that they need like “ammo, auto, name” etc. Because the way I currently have it set up is so I can easily make a new weapon and just put in all the info and call it from a separate script.

Basically the same way that you did it before but instead of having two root tables in your module (which, for the record would not work) you have one with two tables inside it as seen in the code snippet above.

In your serverscript you would require it like this:

local Module = require(script.WeaponHandler)
local Primaries = Module.Primaries
local Secondaries = Module.Secondaries

--Then, just do Primaries.new() or Secondaries.new() after setting everything up in your modulescript for OOP.

Still beyond lost.

--// Module Script \\--
local Weapons = {}

Weapons.Primaries = {name, damage, ammo , auto}
Weapons.Secondaries= {name, damage, ammo}

This doesn’t work. Idk where to even start with this way of doing it.

You could separate them into two individual module or pass an option (Primary / Secondary) as an argument to the constructor.

2 Likes