How to save Dictionary using DataStore2?

I’m not too sure on how Datastore2 works but if you haven’t already the creator of it provided examples and there’s a tutorial on it too: How to use DataStore2 - Data Store caching and data loss prevention

I don’t want to give you misleading info/things I’m not 100% on but I’m pretty sure, after reading the thread, you’d just call Set() and within the parameter provide your table. Save() is automatically called upon the player leaving. You also want to load the data upon the player entering the game (PlayerAdded event). Hope this helps!

1 Like

There is no “Datastore2 way”. You do it as you normally would with data stores. (Get data, modify table, save data)

1 Like

Instead, I’d save IDs. Loading the IDs will start looking through a dictionary of what the ID corresponds to which value.

Saving a dictionary is possible. If you bump into an error, PM me.

1 Like

As an example, let’s say you wanted to save a player’s inventory, but you wanted to optimize the save. For example, if a player has an inventory of {"Item1", "Item2", "Item3"} , and you want to save it as {1, 2, 3} . You can do this easily with these functions. Your code may look like this.
-Kampfkarren

Well, he doesnt explain how to do it, only how to optimize a table, nothing even with dictionaries.

But thank you for your time!

Are you sure about that? Have you used DataStore2?

Could you clarify?

If I understood you right i’d need 2 dictionaries.

One with the IDs and one with the corresponding items, this seems like more work than I have to do with only saving a dictionary.

Let’s put an example:

-- saving table
local saveTable = {
    1, 2, 3, 4
}

-- reading dictionary
local readDictionary = {
    [1] = { -- data for 1};
    [2] = { -- data for 2 and so on};
}

Saves a bunch of characters away(limit is 260k, but it is closely to unlikely to hit that limit). Perhaps saving the dictionary directly will do, until you find out that your game is saving a lot of data.

Yes, it would work. But I think it is easier just to save the dictionary in the DataStore and if you are worried about the saving limit I could just JSON-encode the table and then save it.

1 Like

Um, yes I’m sure. It’s pretty simple.

Have you done it before?
I just have a hard time believing it since, you save for example coins a different with other functions in DataStore2.

I have no clue what you’re talking about. DataStore2 is just a wrapper for DataStoreService with extra functionality like data backups, combined datastores, as well as QOL features like getting data with a default value.

There is no difference when saving dictionaries. Like normal DataStores, there can only be string keys.

2 Likes

if I’m correct, you’re trying to save a collection of data (the dictionary) under one datastore, instead of saving single values to multiple datastores. Here’s an example of how you might do that (includes retrieving the data, modifying and setting the data):

-- Services
local players = game:GetService("Players")
local dataStore2 = require(1936396537)
local userDataStoreName = "UserDataStore"

--Get data (retrieves previous data or sets-up new data using the setupUserData function)
local function setupUserData()
	local userData = {
		
		}
	return userData
end
local userData = dataStore2(userDataStoreName, player):Get(setupUserData())

--Modify data
userData["Currency"]["Gems"] = 9999

--Set data
dataStore2(userDataStoreName, player):Set(userData)

Make sure to replace local userData = {} in the setupUserData function with your dictionary.

41 Likes

Thank you so much for the answer! Testing it out now!

Hi, this might be a silly error, but somehow I dont manage to save the values off: userdata["Currency"]["Gems"] (I can change them, but not save them for some reason.

This is my full script:
– Services
local players = game:GetService(“Players”)
local dataStore2 = require(game.Workspace.MainModule)
local userDataStoreName = “UserDataStore”

--Get data (retrieves previous data or sets-up new data using the setupUserData function)
local function setupUserData()
	local  userData = {
	
	Currency = {	
	["Gems"] = 15;
	["Coins"] = 1500;
	};
	
	
	Items = {
		
		["Weapons"] = {
		["Knife"] = {nil};
		["AK-47"] = {nil};
		["M9"] = {nil};
		};
		["Clothing"] = {nil};
		};
	
	WeaponsEquipped = {
		["Main"] = {"AK-47"};
		["Secondary"] = {"M9"};
		["Melee"] = {"Knife"};
		["Special"] = {nil};
		};
		
	ClothingEquipped = {
		["Pants"] = {nil};
		["Hats"] = {nil};
		["Shirts"]=  {nil};
		["Face"] = {nil};
	};
}
	return userData
end

game.Players.PlayerAdded:Connect(function(player)
	local userData = dataStore2(userDataStoreName, player):Get(setupUserData()) --skrev userdata = userdata noe sånt sjekk! 
	print(userData["Currency"]["Gems"])
end)

game.Workspace.Baseplate.ClickDetector.MouseClick:Connect(function(player)
local userData = dataStore2(userDataStoreName, player)
userData["Currency"]["Gems"] = userData["Currency"]["Gems"] + 10
dataStore2(userDataStoreName, player):Set(setupUserData())
print(userData["Currency"]["Gems"]) [[--this prints the gems value (incremented by 10 --everytime i click)]]--
end)

I just made this code to check if it saves, but it doesnt :frowning: Would really appreciate it if you would help me! And dont know if the error is in the save or the retrieving of data but yeah.

Sincerely.

You’re setting the wrong value:

dataStore2(userDataStoreName, player):Set(setupUserData())

should be:

dataStore2(userDataStoreName, player):Set(userData)

In your example, you were saving the values returned from the setupUserData() function instead of the userData.

7 Likes

Oh :expressionless:

I have to become better at finding my errors in scripts :sweat_smile:

Thanks, I will test it out as soon as i
i get home. Thanks!

1 Like

Hey, did you get it working? Im still having troubles with saving and loading with dictionaries

Hi, I got it working. But ages since I used it, so dont remember alot. But what is your issue?

I think I got it figured it out, but having troubles saving in studio.

Sorry for not telling you, but it is NOT possible to save in studio! Sorry! You’ll have to test in-game.