OOP Inventory System

I have been working on my own inventory class for the past couple of days when I noticed something in my code

-- Removed most of the functions and the  code in each function
-- just to make it easier for the reader to analyze 
 
local ServerInventories = {}

local InventoryClass = {}
InventoryClass.__index = InventoryClass

local Inventory = {}


------------------
--[Functions]--
------------------

function Inventory.New(player, items) 
	
end


function Inventory.Get(player) -- returns the player inventory 

	
end


-----------------
--[Methods]--
-----------------

function InventoryClass:AddItem(item, amount)

end


function InventoryClass:RemoveItem(item, amount)
	
end


so you can see here that I’m using three tables, One for the server inventories which basically have all players items, one for the inventory class which has all the methods inside, and lastly the one which the module will return with three functions,

so my question is Cant I just combine the module table with the class table and return the class table instead of the module table Keeping in mind that this module will be required on more than one server script since I heard about some possible errors that could happen from this in an old topic but unfortunately I can’t find it anymore to ensure my information. Thx in advance.

so it would look something like this:

local ServerInventories = {}

local InventoryClass = {}
InventoryClass.__index = InventoryClass



function InventoryClass.New(player, items)
	if not items then --set default inventory 
		
		items = {}
		
	end
	
	local playerInventory = setmetatable({ 
		
		Items = items,
		Owner = player
		
	}, InventoryClass)
	

	ServerInventories[player] = playerInventory
	
	return playerInventory	
end


function InventoryClass.Get(player)
	
	
end




function InventoryClass:AddItem(item, amount)
	

end


function InventoryClass:RemoveItem(item, amount)
	
	
end





return InventoryClass



I’m not sure what you’re asking here. Are you familiar with classes in general? Because you can index self in methods and such.

function InventoryClass.new(player, items)
	items = items or {} --set default inventory 
	
	local playerInventory = setmetatable({ 
		Items = items,
		Owner = player
	}, InventoryClass)
	
	ServerInventories[player] = playerInventory
	
	return playerInventory	
end

function InventoryClass:AddItem(item, amount)
	print(self.Items, self.Owner)
end
3 Likes

both the ServerInventories and Inventory tables arent needed, for Inventory.New and .Get you should use the InventoryClass, and the ServerInventories table can be swapped with _G, this should make the code for getting a players inventory much cleaner and negates the need for .Get

eg:

local HeldItems= require(ServerStorage.ServerModules.HeldItems)

_G["HeldItems"..plr.UserId] = {}
_G["HeldItems"..plr.UserId][1] = HeldItems.New(plr)
_G["HeldItems"..plr.UserId][1]:Set("Container")

Obviously you would need to clean up these variables when a player leaves to save on memory

1 Like

while this can be another way of acomplishing the same thing but there are a lot of topics talking about uisng _G in your code as local players can modify it , its only a matter of time before they figure out the name of the _G variable to start adding and removing items from their inventories,

really appreciate your answer but i just wanted to know if i can relocate the module table functins inside the class so i would have one table with all the methods and another table for players items
and just remove the module table

i think i just understood what you meant, thx mate now i can combine them both

Using _G in LocalScripts is not a good thing to do.
When you use them in Scripts though, no exploiter can do anything with them.
So feel free to use _G in Scripts, but don’t use them in LocalScripts.

2 Likes