If your intention is to store an inventory in the format of value instances you will end up with a laggy game and it is not a correct way to create an inventory if your inventory is going to be "infinite" or scalable.
Here is the catch, you can use tables, they are a way better solution to the inventory problem, they offer modularity and they are infinitely scalable, you can store a long table for a user and occupy a lot less information compared to having them stored with instances.
Here is an example of a dummy table:
local table_inventory[player.Name] = {
["Sword of Scripting"] = {
Quantity = 1,
Rarity = "Epic",
Durability = 1337
};
["Shield of Protection"] = {
Quantity = 32,
Rarity = "Common",
Durability = 1
};
};
As you can clearly see you can store all the information that you need inside a table and retrieve and remove information as long as you have an index / key with which you can select the item and manipulate it.
The functions that you use to manipulate the information of a table are table.insert() and table.remove()
You can store the table inside a DataStore and you will do not need to worry about saving instances and lagging your place. Just picture that if a person has 9000 instances and you multiply that player by 12, that is 108,000 instances loaded in your place just to store 12 people's inventory space. That is a lot of lag to your place and unnecessary instances.
Tables are good because you can save all the relevant information inside the table inventory of the user and then if you want to retrieve the item from his inventory you would need to modify the table information such as the quantity and then use the:Clone()method to clone a sword for the player in an actual physical storage inside the game, that way you can have less items inside the physical storage of the player and control the bigger storage in the actual table.
Make sure to store all the relevant information in the table and not the actual instances because if you intend to save your inventory with DataStore, remember that DataStores do not store instances.
Here are some useful links that could help you in your journey on learning how to make an inventory with tables:
https://www.lua.org/pil/2.5.html
https://developer.roblox.com/en-us/articles/Table
And here is a link to DataStores for you to also check them out:
https://developer.roblox.com/en-us/articles/Data-store
If you have any other questions, feel free to ask!
:)
4 comments
First of all really thanks for such good answer :) But from what I understand you showed example where an item has fixed values but items in this game won't have fixed values like for example "Swords of heavens" won't always have 100 power but it will vary between 80 - 120. Correct me if I didn't understand something. Also can DataStores save tables?
You can change any property on your inventory table on whatever item you want and whenever you want, correct. After that you can physically retrieve the item to your inventory whenever the player requests it with :Clone() in case he wants to see the sword in-game, you check if he has it in the inventory table, substract the sword from there whenever he requests to equip it and clone
the real sword into the user's backpack for example. And whenever he unequips it, you just insert the item back again in the inventory table. And answering to your DataStore question, yes you can save tables inside DataStores as long as inside the table you are not saving any kind of "instance", for example a part. Make sure to save relevant information inside the table that you can utilize
to retrieve the item physically inside the game whenever needed. Have a good day. :)