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

How do you save a table to a data store?

Asked by
RedCombee 585 Moderation Voter
10 years ago

I'm disappointed by the lack of example code ROBLOX put on the wiki for DataStores, and I need to find out how to do this properly.

2 answers

Log in to vote
1
Answered by 10 years ago

This completely depends on how you want to save it. Also, your question doesn't make it quite clear which part you do not understand! Here's some example code.

table = {"Entry1","Entry2","Entry3"} -- This is the table we will be saving.
ds = game:GetService("DataStoreService"):GetDataStore("NameOfDatastore") -- allows us to use datastore specific methods. you can add a scope if you want.

for index,value in pairs (table) do
    ds:UpdateAsync(index, function() return value end) -- index will be our 
end 

This will save it to a datastore. You can read it out by using :GetOrderedDataStore("NameOfDatastore") but that's a different question ;)

0
This is not recursive and only works for stringy keys, does it not? BlueTaslem 18071 — 10y
0
How else should I approach this? RedCombee 585 — 10y
0
Indeed, it is not recursive. You can also just save the whole table as a value and give it a keyname. juzzbyXfalcon 155 — 10y
0
My data is currently as some instances, so would I have to save all the data under strings since you can't save instances directly? RedCombee 585 — 10y
0
Indeed, you cannot save instances. You'll have to serialize them first. juzzbyXfalcon 155 — 10y
Ad
Log in to vote
-1
Answered by 7 years ago

the script may contain 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")

Answer this question