Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

[SOLVED] How do you save complex tables?

I have a simple datastore which can save the name of a StringValue and load it back to the player using:

--// Just the saving
local inventoryFolder = plr:FindFirstChild("inventory")

if inventoryFolder then
	local InvToSave = {} 
	
	for _, v in pairs(inventoryFolder:GetChildren()) do
		if v then
			table.insert(InvToSave, v.Name)
		end
	end
	
	if InvToSave[1] then
		--// Saves the StringValue name to load later
		Inventory:SetAsync(plr.UserId, ActualInventory) 
	end
end

I do it this way because all the players have a physical folder called inventory. Probably not best practice but easier for me.

My question is how would you also save the StringValue value?

Using this example table below, I can only save Apples and Oranges but I also want to save the extra data they contain which would be the StringValue value.

local inventory = { 
	Apples = {ExtraData = “Red”}, 
	Oranges = {ExtraData = “Orange”} 
}

2 answers

You can encode tables & dictionaries into JSON and convert them back using HTTPService.

String JSON = HTTPService:Encode(Table Table) takes the table, and returns the JSON

Table Table = HTTPService:Decode(String JSON) takes the JSON string and converts it back into a table/dictionary.

You can then save the JSON string into roblox's datastores with SetAsync or UpdateAsync, or any other ways you have to do so.

Found a solution. If anyone is interested in the solution to this later on:

--// Save inventory
local inventoryFolder = plr:FindFirstChild("inventory")

if inventoryFolder then
	local InvToSave= {}
	
	for _, v in pairs(inventoryFolder:GetChildren()) do
		local dataTable = {}
		dataTable.ItemName = v.Name
		dataTable.ExtraData = v.Value
		InvToSave[#InvToSave+ 1] = dataTable
	end
	
	if ActualInventory[1] then
		Inventory:SetAsync(plr.UserId, InvToSave)
	end
end

This discussion is preserved as it was. New answers, comments, and votes are closed.