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

How do you save data stores within a table?

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

2 comments

0

Try using JSON Encode and Json Decode

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

1 answer

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.

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