While the OP doesn’t understand MetaTables (therefor it may be best he doesn’t use them) I would have to disagree with this. One of the main uses of a metatable is when you’re going to be indexing a class, in this case an Inventory should be unique to each user and if you want to make your life easy then you could set up a MetaTable like I did in this example.
local Inventory = {}
local InventoryClass = {}
local Inventories = {}
function InventoryClass:AddItem(Item, Amount)
local itemName = Item.Name
if not self.Contents[itemName] then
self.Contents[itemName] = {
Amount = Amount,
Tool = Item
}
else
self.Contents[itemName] = self.Contents[itemName].Amount + Amount
end
end
function InventoryClass:GetContents()
return self.Contents
end
function Inventory:GetInventory(Player)
return Inventories[Player]
end
function Inventory.New(Player, Table)
local InventoryMeta = setmetatable({
Contents = Table,
Ownership = Player,
}, {
__index = InventoryClass
})
Inventories[Player] = InventoryMeta
return InventoryMeta
end
return Inventory
Anyways, that’s besides the point.
A good thing to keep in mind is that when making an inventory system you don’t need to worry about what slots items are in, as long as they’re in the inventory table.
Example: I have a table that contains two pieces of wood, that’s in my fourth slot, I iterate through the inventory table and check to see if my inventory contains two pieces of wood (this is to prevent exploiting) if it does then the user can use that to craft, etc…
Now if for some reason you truly need to store the slot, you could do this by using slot ids, as long as all of the slots don’t change names (like the image label) then you can store a value inside of your inventory table of each item.
With this is mind let’s get scripting something that doesn’t use MetaTables.
I would strongly recommend making this all in a ModuleScript.
We first need to define our Inventories, this will be a table that contains every single users inventory.
local inventoryHandler = {}
local inventories = {} -- This will be a dictionary with the player being the key
return inventoryHandler
Now when a player joins there inventory will be empty, I’m not going to go over how to datastore an inventory as that’s a completely separate tutorial. We’ll simply be initializing a blank table when a player joins the game.
local inventoryHandler = {}
local inventories = {} -- This will be a dictionary with the player being the key
function inventoryHandler:CreateInventory(Player, Inventory) -- Inventory could be a datastored table, etc..
if not Inventory then -- Checking to make sure that Inventory was defined
Inventory = {} -- Inventory wasn't defined therefor we'll make a blank table
end
inventories[Player] = Inventory -- Setting up the player's inventory inside of our inventories table
return inventories[Player] -- Returning the inventory incase it needs to be used later on
end
return inventoryHandler
Awesome, now our inventory is created!
Let’s now add some items, it’s pretty easy! Keep in mind that this is where we will define things such as the slot an item is in (or any item data)
We will name our item’s data itemData. This is an example of what the table will look like.
{
["Name"] = "Stone",
["Amount"] = 15,
["Slot"] = 5,
}
Here’s a basic function we can use to add items.
function inventoryHandler:AddItem(Player, itemName, itemData)
if not inventories[Player] then
inventories[Player] = {}
end
local inventory = inventories[Player]
if not inventory[itemName] then
inventory[itemName] = itemData
else
inventory[itemName].Amount = inventory[itemName].Amount + itemData.Amount
end
return true
end
Removing items is the same as adding items just with a few changes, so I won’t be showing that.
Be sure to remove the players inventory table when they leave, etc…!
If you want to display the items now based on the slot then you can simply get the inventory (via invoke, etc…) and then display it based on the Slot value.