Accommodating for Multiple Stacks of Items in an Inventory System

try not to nest so much in one function, you can always create sub-functions

1 Like

Yep, exactly that’s how my previous system was set, basically you will have an item data table generally in the format:

Item = {
    ID: string,
    Amount: number

And you would assign it to a slot like:

local Inventory = {}

Inventory[1] = {
    ID = "Sword",
    Amount = 1,

would that still be compatible with the tbl variable in the for loop?

This is a bad idea cuz it offers less flexibility as later on you myt decide to scale the system and this will become a hindrance. It’s better if you followed an OOP approach by creating an inventory instance for each player.

What’s an OOP? Never heard the term before

I realized the top of the script wasn’t put in the original post. Here’s what I did at the top:

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

	local PlayerKeycodeID = Player.UserId
	local PlayerInventory = table.create(0)
	PlayerInventory["PlayerKeycodeID"] = PlayerKeycodeID

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

    local self = {}

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

Whenever our player joins, we would do:

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

	local inventory =
    InventoryTables[player] = inventory

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]

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?