[DataStore] Save table with dictionary and index

I was in the studio yesterday testing UpdateAsync for some features in my game, and I was trying to save a table and it repeatedly wouldn’t save, at least not the dictionary part. So I thought it was a problem with UpdateAsync, so I switched to SetAsync and the problem continued. I’ll leave the details below.

If you create a Script and insert this code, you will see that the dictionary will not be saved.

DATASTORESERVICE = game:GetService("DataStoreService")
DATASTORE = DATASTORESERVICE:GetDataStore("Test")

local DATA

local SAVEDATA1 = {
	[1] = 111,
	[2] = 222,
	[3] = 333,
	[4] = 444,
	Informations = {
		Test1 = 123,
		Test2 = 321
	}
}

local SAVEDATA2 = {
	Informations1 = {
		[1] = 111,
		[2] = 222,
		[3] = 333,
		[4] = 444
	},
	Informations2 = {
		Test1 = 123,
		Test2 = 321
	},
}

local S, E = pcall(function()
	DATASTORE:SetAsync(123456, SAVEDATA1)
	DATA = DATASTORE:GetAsync(123456)
end)

if S then
	print(DATA)
end

However, if you change the Script to save the SAVEDATA2 table, which does not have an index, but rather 2 dictionaries, you will see that both are saved.
I really believe this is a bug, because it doesn’t seem like something that should happen.

3 Likes

I think I know your issue. Let me test some things real quick and get back to you! :smile:

Okay try this:


DATASTORESERVICE = game:GetService("DataStoreService")

DATASTORE = DATASTORESERVICE:GetDataStore("Test")

local DATA

local SAVEDATA1 = {}

SAVEDATA1["1"] = 111

SAVEDATA1["2"] = 222

SAVEDATA1["3"] = 333

SAVEDATA1["4"] = 444

Informations = {}

Informations["Test1"] = 123

Informations["Test2"] = 321

local SAVEDATA2 = {}

local Informations1 = {}

local Informations2 = {}

Informations1["1"] = 111

Informations1["2"] = 222

Informations1["3"] = 333

Informations1["4"] = 444

Informations2["Test1"] = 123

Informations2["Test2"] = 321

SAVEDATA2["Informations1"] = Informations1

SAVEDATA2["Informations2"] = Informations2

local S, E = pcall(function()

     DATASTORE:SetAsync(123456, SAVEDATA1)

     DATA = DATASTORE:GetAsync(123456)

end)

if S then

     print(DATA)

end

local S, E = pcall(function()

     DATASTORE:SetAsync(123456, SAVEDATA2)

     DATA = DATASTORE:GetAsync(123456)

end)

if S then

     print(DATA)

end

It should successfully print both tables. And you can get data from them by writing:

SpecificDataThatIWantFromTheTable = DATA["The name of the table item"]

So for example, you could write DATA[“1”] or DATA[“Informations1”] or since the Informations1 is a table thats part of the DATA table, you could write DATA[“Informations1”][“1”]

You have to use JSONEncode

local DATA
DATASTORESERVICE = game:GetService("DataStoreService")
DATASTORE = DATASTORESERVICE:GetDataStore("Test")
local SAVEDATA1 = {
	[1] = 111,
	[2] = 222,
	[3] = 333,
	[4] = 444,
	Informations = {
		Test1 = 123,
		Test2 = 321
	}
}
local S, E = pcall(function()
   DATASTORE:SetAsync(123456, game:GetService("HttpService"):JSONEncode(SAVEDATA1))
end)
if S then
   print(game:GetService("HttpService"):JSONDecode(DATASTORE:GetAsync(123456)))
end

Also, you do have “Studio Access to APIs” enabled right?

I had already seen that Roblox itself converts the table to JSON when saving the data. I did what user Chloe suggested and what I sent continues to happen. You can see that the print in Studio will continue with a table:

{
[1] = 111,
[2] = 222,
[3] = 333,
[4] = 444
}

The dictionary was lost for some reason and was not saved. Maybe some internal method that Roblox is using to analyze if the data has Index or Dictionaries?

This error I encountered is not exactly a problem for me, because in fact the way the data was saved was accidental. However, this occurrence was good because it revealed this small problem (which I believe to be). It would be good if a member of the Staff could verify if there is in fact a bug occurring in this type of situation.

What Dr Duck said actually works, if it is not a numeral Index but rather a string it will save the data. This is another reason why I believe it was not supposed to happen this way.

Well, does that mean this topic is resolved? Glad I could help in any way whatsoever! :+1:

This seems to be a limitation with how the data is converted into JSON or whatever format Roblox uses on the backend, it is documented with Remote and Bindable Events:

Table Indexing

If you pass a table of data, do not pass a mixed table of numeric and string keys. Instead, pass a table that consists entirely of key-value pairs (dictionary) or entirely of numeric indices.
⚠️ Whether passing a dictionary table or a numerically indexed table, avoid nil values for any index.

(Source)

This was the information I was looking for. I figured this would be some sort of bug and no prior notice that needed to be reported, but it seems like this is the way it should be.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.