How to Save a Table Within a Table

I’m trying to find a way to save tables. I have gotten it to save, but any table within the table being saved does not get saved with it, and all of its data is just transferred to the main table being saved. How do I fix this?

Edit: For context, this is what the saved data looks like in the output:

DATA =  ▼  {
                    [1] = 0,
                    [2] = 10,
                    [3] = 0,
                    [4] = 0,
                    [5] = 10000,
                    [6] = 0,
                    [7] = 1,
                    [8] = 1,
                    [9] = 1,
                    [10] = 1,
                    [11] = 0,
                    [12] = 0,
                    [13] = 0.5,
                    [14] = 0,
                    [15] = 0.05,
                    [16] = 40,
                    [17] = 5,
                    [18] = 1,
                    [19] = 100,
                    [20] = 10,
                    [21] = 0,
                    [22] = 0,
                    [23] = 0,
                    [24] = 0,
                    [25] = 0,
                    [26] = 0,
                    [27] = 0
                 }

There are no names, just the number of their order and the value.
Each value should have a name, and they should all be seperated into their own subfolders within the main one.

You could encode it to a JSON array before saving the data and then unpack it when you need it again.

When in the saving process should I encode the table?

You should encode the table right before saving it, since you can’t exactly make changes once you’ve done it.

1 Like

Now the output is:

DATA = [0,10,0,0,10000,0,1,1,1,1,0,0,0.5,0,0.05,40,5,1,100,10,0,0,0,0,0,0,0]

There still is no name or subtable present
Also, for some reason, they have also been coming out in an odd order.

Hello, If you are using datastore when changing the value you must give SetAsync().

To get more than that, it would be nice if you send me how you’re saving.

Here is what I am doing:

function Save(player)
	local success, errormessage = pcall(function()	
		
		myDataStore:SetAsync(player.UserId.."-DATA", json)
	end)

	if success then
		print("Data Successfully Saved.")
	else
		warn("There was an Error Saving Data",errormessage)
	end
end

(json is the encoded table mentioned earlier in this post originally it was just the table, but the json doesnt seem to affect anything besides the data now being listed on one line instead of one line per value)

In the script that itself changes a value you could do something like this:

local myDataStore = game:GetService("DataStoreService"):GetDataStore("YourDataStore")
local key = "plr-"..plr.userId
local saveddata = myDataStore:GetAsync(key)
	
saveddata[1] = "10101"
	
myDataStore:SetAsync(key,saveddata)	

(Note: I have not tested)

Roblox automatically encodes table values in JSON format when being saved to a ‘DataStore’ instance.

2 Likes

Circling back to the beginning of my post, how do I get the subtables to be saved, and not have their data be transferred into the main table? My game requires these sub-folders, as each player can pretty much have an infinite combination of table data including:

  • Integer Values
  • Models
  • String Values

If I just try to use one table, there is no way to know what is what within it, as Player1 might have X models, and Player2 might have Y. The only consistent way to do this is to have these sub-tables that I am trying to get to save with their main parent table. And the names of the values are not saved, just the value itself, so there is no way to know what value is which when it changes throughout gameplay. These values also are not in the same order as they are listed in the script.

Is there a way to do all of this a different, more reliable way with folders or something?