Would it be better to separate this into two module scripts?

Would it be better to separate this into two module scripts? One would create a new item and include any additional methods. While the other initializes the module scripts, each containing data for the item, and includes a get method to retrieve the item data.

local Item = {}
Item.__index = Item

local _data = {}

function Item.new(itemData: table)
	local self = setmetatable({
		Name = itemData.Name,
		Icon = itemData.Icon,
		SellPrice = itemData.SellPrice,
		StackSize = itemData.StackSize,
		Quantity  = itemData.Quantity,
		CanStack = itemData.CanStack,
		CanHold  = itemData.CanHold
	}, Item)
	return self
end

local function _Init()
	for _, module in script:GetChildren() do
		local newModule = require(module)
		_data[module.Name] = newModule
	end
end

function Item.Get(itemId: string)
	return _data[itemId]
end

function Item.IsATool(item: table): boolean
	if item.Model == nil then return false end
	if item.Model:IsA("Tool") then
		return true
	end
end

_Init()

return Item
2 Likes

If you mean that you should seperate it into 2, one of them being the item module script and the other holding the data then yes, i always like to have my data on 1 module script that manages data so its easier for others scripts to access it. However, if no other script will use it then imo i dont think you should do it
Its just a preference

3 Likes

similar to a module loader with the init function if you plan on making other modules with the init function then you should make a module loader

2 Likes

I mean, maybe, although I would only do that with a longer script.

2 Likes