Can you add a table to a table?

I am trying to make a save system that uses only one key, and in order to do this I need to have a table inside a table.

function saveData(player)
	local plrdata = {}
	
	plrdata.Coins = player.leaderstats.Coins.Value
	plrdata.Stars = player.PlayerStats.Stars.Value
	plrdata.Slots = player.PlayerStats.Slots.Value
	
	local playerblobs = {}
	
	for i,v in pairs(player.Blobs:GetChildren()) do
		playerblobs[v.Name] = {}
		local lb = playerblobs[v.Name]
		lb.Level = v.Stats.Level.Value
	end
	
	table.insert(playerblobs, plrdata)
	
	playerdata:SetAsync(player.userId, plrdata)
end

However, when I do this, then I print everything in the save nothing is there. Is there another way to do this?

3 Likes

Are you trying to insert playerblobs into plrdata? If so, you have table.insert’s arguments mixed up. The first argument is the table you want to put the second argument in.

6 Likes

Ha! You’re right.

I’m such an idiot sometimes, thank you.

You cannot use table.insert for this iirc, as it is intended for use with arrays. Datastores expect the tables you’re saving to either be completely indexed by string, or by integer, not mixed together.

You should instead do something like plrdata.Blobs = playerblobs.

2 Likes

table.insert(playerblobs, plrdata)

Not only are the two parameters switched, but you’re going to end up with a table with numerical and string keys. Since tables with mixed string and numerical keys can’t be encoded into JSON (and thus can’t be saved into your datastore), you’re going to encounter a datastore error.

Instead of

table.insert(plrdata, playerblobs)

I would recommend that you do this instead.

plrdata.Blobs = playerBlobs
3 Likes

Thank you!

To read from it, I then use:

for i,v in pairs(plrdata.Blobs) do
print(v.Level)
end

right?

3 Likes

Yep.

1 Like