Is there a better way to organize this

Hello, I have a table that has multiple tables inside of it. I want to save specific objects to the table and I was able to do this with If statements but I feel like there is a way I can make this more flexible but I don’t know how.

Main code:

local SectionTable = {
	S1 = {};
	S2 = {};
	S3 = {};
	S4 = {};
}

local function AddItemToTable(plr,obj)
	local objType = Module[obj.Name]["Type"]
	local objName = obj.Name
	local S1Table = SectionTable.S1[plr.Name]
	local S2Table = SectionTable.S2[plr.Name]
	local S3Table = SectionTable.S3[plr.Name]
	local S4Table = SectionTable.S4[plr.Name]

	if obj == S1Table[objName] or S2Table[objName] or S3Table[objName] or S4Table[objName] then
		if objType == "S1" then
			S1Table[objName] = S1Table[objName] + 1
		elseif objType == "S2" then
			S2Table[objName] = S2Table[objName] + 1
		elseif objType == "S4" then
			S4Table[objName] = S4Table[objName] + 1
		elseif objType == "S3" then
			S3Table[objName] = S3Table[objName] + 1
		end
	else
		if objType == "S1" then
			S1Table[objName] = 1
		elseif objType == "S2" then
			S2Table[objName] = 1
		elseif objType == "S4" then
			S4Table[objName] = 1
		elseif objType == "S3" then
			S3Table[objName] = 1
		end
	end
end

Module Layout:

["Sword"] = {
		Name = "Sword";
		Type = "S1";
		Model = EquipmentModels.Sword;
		Desc = "sword";
    };
4 Likes

I dont see any real way looks all good

You don’t need all those if statements, just do:

SectionTable[objType][plr.Name][objName] += 1

This will dynamically increase each table without the need of if statements(sort of like hierarchy finding).

1 Like

I think it’s pretty good, I personally don’t know much about code organization but I feel like this is pretty organized.

1 Like