Creating multiple different saves for every player with DataStore2

Is such a thing possible? I want to have a system where I can save a new inventory to each character the player creates. I have not been able to think of any way to do this without using the traditional datastore.

What’s the best way to go about doing this?

1 Like

You should be able to save nested tables with Datastore 2 like so:

local Inventories = {
	-- {string Character Name, table Contents}
	{"character1", {{"Apple", 3}, {"Bronze Sword", 1}}}, 
	{"character2", {{"Iron Pickaxe", 1}, {"Bow", 1}}}
}

PlayerInventoryDatastore:Set(Inventories) -- I personally don't use Datastore2 so I'm not sure if this is the correct method but you get the idea

Yeah, like @metryy said you’d just create a nested table for each character. This link shows how to save dictionaries with datastore2 which is the same concept.

What if I wanted to save something like coins for each character? Isn’t using set over and over bad when I could be using :Increment for something like that?

Is there a way to :Increment the coins if it’s in a table containing all the data?

There is not a way to increment a value in a table I believe (but I haven’t tested). Setting a value is not bad as long as you’re not doing it a bunch of times per second, so if a player is just changing their character’s coins every 10 seconds or something you’ll be fine.

2 Likes

If I set a table twice at the same time (once for updating coins, once for updating inventory) would it risk overwriting anything? If so, wouldn’t I have to save player data when the player leaves the game which defeats the purpose of datastore2?

Can I somehow fix this by using :update on tables or something?

Sorry for the super late response, but no it wouldn’t as long as your not saving in the same function. If your saving in the same function you should only set it once. You never have to save data when leaving with datastore2.

1 Like