Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do you save data stores within a table?

Asked by 7 years ago

I'm trying to save a bunch of boolvalues that are in a folder called GoldPlayers in Workspace without having to save each one individually...please help. Thanks, Bylocks

0
Try using JSON Encode and Json Decode drslicendice 27 — 7y
0
In case my answer wasn't clear, you save the results of Encode to a datastore, then later you load whatever's in the datastore using Decode chess123mate 5873 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

Use JSON to encode a list of true/false. Since the order of GetChildren is not guaranteed, you may want to also save the name of the BoolValue you are saving (so the first entry in the table will be the name of some BoolValue, the second entry will be its value, the third entry will be the next BoolValue's name, and so on).

local HttpService = game:GetService("HttpService")

function Encode()
    local values = {}
    local ch = workspace.GoldPlayers:GetChildren()
    for i = 1, #ch do
        table.insert(values, ch[i].Name)
        table.insert(values, ch[i].Value)
    end
    local json = HttpService:JSONEncode(values)
    return json
end
function Decode(json)
    local values = HttpService:JSONDecode(json)
    for i = 1, #values, 2 do
        local obj = workspace.GoldPlayers:FindFirstChild(values[i])
        if obj then
            obj.Value = values[i+1]
        else
            warn(tostring(values[i]) .. " not found in GoldPlayers")
        end
    end
end
--Save the results of Encode to the datastore and load it back using Decode

As you can see, with the names having been stored, the Decode function can be guaranteed to return each True/False to the correct BoolValue.

(This code assumes that all your BoolValues will have different names).

Naturally you will have to connect this code to your DataStore code.

Ad

Answer this question