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

How do I save a table to Data Store?

How would I save a table using SetAsync?

2 comments

1

Go to roblox wiki.

0

I don't know what the people above me are talking about with HTTP Service and stuff. Just save it like anything else. http://prntscr.com/ma4rd3

3 answers

You can use the http service. The script may has some error:

local table = {a = "0",b = "0"} -- The main table
local ds = game:GetService("DataStoreService") -- datastore service
local hs = game.HttpService -- the http service
local nds = ds:GetDataStore("test")
local value = hs:JSONDecode(nds)
if value == nil then -- if it the first time you start the game then you start with the main table
	value = table
else
	value.a = value.a + 1 --we want to add 1 to a every time
	value.b = value.b + 2 --we want to add 2 to b every time
end
local encode = hs:JSONEncode(value)
hs:SetAsync("test",encode) -- may be it (encode,"test")

You would save it as you would another type of data, but do remember the 65536 character limitation and that it saves the table encoded in JSON, to get the length of the encoded table length, doprint(string.length(game:GetService('HttpService'):JSONEncode(table)))

Example:

game:GetService('DataStoreService'):GetDataStore('AName'):SetAsync('table',{'a','b','c'})
local t = game:GetService('DataStoreService'):GetDataStore('AName'):GetAsync('table')

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