My game has an inventory system. The size of inventory is infinite which means I can't save each slot separately(as there are never-ending) and also each item has individual stuff in them so it adds to the number of values that need to be saved.
The inventory system looks like this:
-Each player has 'Inventory' folder
-This folder has folders for each slot named by number '1', '2, '3' etc.
-Each slot folder has an item that has 3 IntValues: Power, Speed, Ability
Everything is in the prototype stage so I don't have much of code for now. I have only made infinite scrolling frame that adapts to the number of slots used.
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!
:)