Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

[SOLVED] How do you save complex tables?

Asked by 2 months ago
Edited 2 months ago

I have a simple datastore which can save the name of a StringValue and load it back to the player using: ```lua --// 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. lua local inventory = { Apples = {ExtraData = “Red”}, Oranges = {ExtraData = “Orange”} }

2 answers

Log in to vote
0
Answered by
RubenKan 3473 Moderation Voter Administrator Community Moderator
2 months ago
Edited 2 months ago

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.

Ad
Log in to vote
0
Answered by 2 months ago

Found a solution. If anyone is interested in the solution to this later on: ```lua --// 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 ```

Answer this question