Inventory System (With datastore)

Howdy, so I’m looking to make an RPG Inventory, and I’m dead out of ideas and/or how to save/load tables properly for what I’m wanting to do.

So what I’m looking to do is:
Retrieve a table that gives the player’s gold (singular value) and items.
I’m currently using a module to retrieve an item’s Id, Name and description.
Then create a folder which contains the player’s gold and items.

(I’m competent I can handle the display of the items and the equipping of said items, but as said above, I’m stuck on loading and saving tables and data within said tables.)

Any help, would be appreciated.

Current module to retrieve items is as:
image

(Wouldn’t know if this module would work or not either, still very new to datastore and inventory systems)

6 Likes

If you are giving each item its own unique ID like above, you could save the item ID when saving the player’s data.

Something like this would work:

Datastore:UpdateAsync(PlayerKey, function(Data)
    -- Use currently saved data if already there, otherwise create new table
    Data = Data or {}
    Data.Inventory = {1, 2, 3, 4} -- Store the ID's of your items that the user owns
    return Data
end)

Alternatively, if each item has its own name, you can do the same thing except using the item’s name instead

When retrieving your items for a player, you can do the following:

local Data = Datastore:GetAsync(PlayerKey) or {} -- Load empty table if nothing found
if Data.Inventory then
    for _, ItemId in pairs(Data.Inventory) do
        -- Handle logic
    end
end

This will allow you to save and load a player’s inventory

P.S. With the code you posted in the OP, you can move your Item list out of the function like below:

local Items = {
    {1, 'Name', 'Description'}
}

function module.GetItem(item)
    return Items[item]
end
6 Likes

local httpService = game:GetService(“HttpService”)

game.Players.PlayerRemoving:Connect(function(player)
local dataToSave = {}
local gold = player.Gold – the value of the players gold

local inventory = {}
for _, v in pairs(player.Inventory) do -- loop through the players inventory
	local item = {}
	table.insert(item, v.Id) -- the id value
	table.insert(item, v.Name) -- the item name
	table.insert(item, v.Description) -- the item description
	table.insert(inventory, item) -- insert item into invertory
end
table.insert(dataToSave, gold)
table.insert(dataToSave, inventory)

dataToSave = httpService:JSONEncode(dataToSave)
-- save the dataToSave string

end)

game.Players.PlayerAdded:Connect(function(player)
local data = ds:GetAsyc(player.UserId)
– obviously test to see if the data is actually there before you do this
data = httpService:JSONDecode(data)
player.Gold = data[1]

for _, v in pairs(data[2]) do
	local itemId = v[1]
	local itemName = v[2]
	local itemDescription = v[3]
	--- do what ever you want with loaded stuff down here. AKA make the item and put it in player inventory
end
end)

– note that since you are assigning unique ids to every item, its pointless to save the name and description. save the id
– and when loading each item, give it its name and description based on the id.
– sorry if this is confusing, i am known to be very bad at explaining things :slight_smile:

2 Likes

There are limitations to what sort of data types that you could save. A way to quickly check if the given dataset is saveable, is by turning it into JSON format.

Note: You won’t be able to save a mixed or cyclic table.