Accommodating for Multiple Stacks of Items in an Inventory System

Ohhhhh yeah I remember some of this stuff from when I used to program in Java. This sounds like it’ll work, like a module script for every player?

Yep, if you want I can provide some code which will get you going in the right track

i don’t get why you’d want to use OOP with an inventory system? you can’t save metatables?

Yes please! This is all very new to me and that would be extremely helpful

Well, who said we were gunna save metatables

well i don’t see the point, you’d make it unnecessarily complex with OOP.

Basically you will have an Inventory class which will handle all your items and related functions like adding items, removing items, and so on. The base inventory class would look smth like this:

local Inventory = {}
Inventory.__index = Inventory

function Inventory.new(player)
    local self = {}

    self.Player = player
    self.Data = {}
    
    return setmetatable(self, Inventory)
end

Whenever our player joins, we would do:

local StackLimits = require(workspace.StackLimits)
local Inventory = require(script.Inventory)
local InventoryTables = {}

game.Players.PlayerAdded:Connect(function(Player)
	local inventory = Inventory.new(player)
    InventoryTables[player] = inventory
end)

And yes, OOP might be a lil bit too complex but the outcome is really neat, because you can just incorporate functions in each inventory and it becomes really easy to do a certain task like adding an item.

local inventory = InventoryTables[player]
inventory:AddItem(item)
inventory:RemoveItem(item)
inventory:GetItems()
inventory:Clear()
inventory:Destroy()

Correct me if I’m wrong, but the first script is supposed to be a module script inside the main script?

A child of the main script but yes, it’s a module script

1 Like

Thank you for all your help. The work now starts!

Alright feel free to hum anytime if you want assistance

1 Like

What’s the __ part for? Or is that a just an example value

It’s how OOP works (metatable stuff). If you wan know more, check out the topic I linked, it explains it better.

Alright thank you. Could this also be stored in Datastores in some way so that the player can come back later and still have all their stuff?

Yep, for that you would just listen for the PlayerRemoved event and then call preferably a method called SaveData inside the Inventory, which saves the Inventory.Data

1 Like

I’ve gotten the error: Module code did not return exactly one value. I don’t think I set it up wrong, unless it needs to be put inside one of those premade module tables.

local Inventory = {}
Inventory.__index = Inventory

function Inventory.new(player)
	local self = {}
	
	self.Player = player
	self.Data = {}
	return setmetatable(self, Inventory)
end

return the module at the bottom

local Inventory = {}
Inventory.__index = Inventory

function Inventory.new(player)
	local self = {}
	
	self.Player = player
	self.Data = {}
	return setmetatable(self, Inventory)
end

return Inventory

Should all the executable code be put inside of the PlayerAdded function? Sorry if all these questions sound stupid

Wdym by executable code?