Best way to store Inventory Items within a datastore

Hey guys! I’m currently making an inventory for my game, and I’m wondering what the best practices are to store items, as I want a good, long-term solution!

Here’s an example of the current one:

local Inventory = {
	
	["Item_Name"] = {
		["Type"] = "Weapon",
		["Stat_1"] = "Damage*10",
		["Stat_2"] = "Speed*-3",
		["Stat_3"] = "Damage*10%",
		["Stat_4"] = "Stamina*25%"
	}
}

( “*” Indicates to split the string there, the rest should be self explanatory)

I’m wondering if doing something like this would work well large scale, with many items. I also want to integrate trading so avoiding duplication is another thing I’m striving for!

I’m wondering if i should also make an item ID system, of which each and every item has a unique identifier.

I’m fairly new to datastores, and I’m just looking for feedback on how to make a more robust data system to store items within an inventory. I’m also using profileservice however I don’t think that is relevant.

Thank you for all responses in advance!

1 Like

I think that you can safely use this data structure because key value limit in data stores is 4,194,304 characters so you don’t have to worry about limitations. You can check how many characters does table have by using this simple code:

local HttpService = game:GetService('HttpService')
local Inventory = {
	["Item_Name"] = {
		["Type"] = "Weapon",
		["Stat_1"] = "Damage*10",
		["Stat_2"] = "Speed*-3",
		["Stat_3"] = "Damage*10%",
		["Stat_4"] = "Stamina*25%"
	}
}
print(HttpService:JSONEncode(Inventory):len()) --in this case output is 117

Obviously ids would make this number lower but using them makes development slower. Hope it helps!

1 Like

Hello! Thank you for the response! Thanks for letting me know that I won’t run into any limits for characters, however, do you know if assigning an ID to each unique item would be useful / help prevent duplication in the future, as the system expands as that’s what i was more concerned about

Oh I was thinking about IDs in the other way but if you want to assign id as an “property” of an item, sure why not. You would have more options of scripting this items which is a good thing.

Sorry if this doesnt help

Saving with one datastore is the best way, having multiple datastores is terrible

I bet u knew that, but idk

Yeah, I am using one datastore

1 Like

I’m wondering if it’d be worth it as i’d have to update a global datastore and obviously that’d count towards the read and write limitations

At this point it’s hard to say if you should use them, personally I use ids only if they are necessary.

And one small note, it’s better to add ids now rather than later when you will have almost everything done. Now you have less changes to make.

1 Like

Yeah, that’s true, I’ll try come up with an id implementation later today. Does it in any way help against duplication?

The properties you’ve given here sound more like a configuration than something you would store inside of player data.

What would be best is to have a module (or a folder of modules) which contains information about your items, things like their id, name, type of item, stats like you have.

What you should store in the player data is just a reference to that item. Why? Say for instance you decide to change the name of that item, or you want to change the base damage. If you’ve got that stored within player data it makes it harder to update.

So as an example:

-- itemNameHereInfo.lua
return {
    id = 123,
    name = "Item Name",
    itemType = "Weapon",
    damage = 20,
    maxAmmo = 30
}

-- playerData
{
    [123] = {
        bonusDamage = 5 -- player bought an upgrade to their weapon which boosts damage
    }
}

Think of it like this, store only ‘state’ of the item in player data, such as if the item is stackable you might want to store how much of the item they have, but the max stack size you would store in your config file, that way you can easily change the max stack size without having to update player data. Player items would be indexed by an item id which never changes as this is your reference to your item config. Other properties like the name of the item and other properties are free to change as you please.

1 Like

Thanks for the reply! I did not consider stacking items! However, my approach was also just adding a certain value or % ontop of a base value like you have, sorry if i forgot to explain that

Also, I’ve seen that id can be taken in two ways

  1. A unique id for each item generated
  2. An id that identifies what an item is

Do you think I would need option 1, as with your example, you have done option 2

Use ProfileService that is very secured and less risk of losing data.

1 Like

Yeah, however, my own implementation / way of storing data can also affect how easy the data is to manipulate in future

1 Like