How to save Dictionary using DataStore2?

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.

40 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.

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

Why are you spreading misinformation? It is possible to save in studio while using Datastore 2, but not sure why any of you would want to.

3 Likes

Relax, I know ot is possible, but as you said why would they want to. I just did not want to confuse him as he was already a little confused.

1 Like

This contradicts what was said earlier.

DataStore API is enabled for Studio and is fine for unit testing. I wouldn’t recommend doing DataStore testing on your main game slot however as it can tamper with actual data you need to work with.

Anyway, this is starting to get off topic. Please move this to another thread or begin a DM if you all wish to continue.

1 Like

Wouldn’t he need to add a .Combine? Or does it combine it automatically?

1 Like

saved my career respect da man

how would you access the table from other scripts?