How to load a table saved in a DataStore?

So I created a Table and stored it in a datastore. I wrote something like that:

local mytable = {}
table.insert(mytable, myThingsIWantToAdd)
DataStore:SetAsync(plr.UserId.."ThingsIAdded", mytable)

It works fine, (I looked in a plugin and checked) but how do I load it? I’ve tried something like this, but it didn’t work:

local myloadedtable = DataStore:GetAsync(plr.UserId.."ThingsIAdded")

I didn’t get any errors, but the loaded table just doesn’t seem to work.

Thanks in advance! :smiley:

Use HttpService and use the JSONEncode/JSONDecode.

Example:

local HttpService = game:GetService("HttpService")
local DS = game:GetService("DataStoreService")
local gameDS = DS:GetDataStore("SampleDS")


-- Loading
local sampleKey = gameDS:GetAsync("key_001");
local getData = HttpService:JSONDecode(sampleKey) -- Becomes table

print(getData.Money) -- 12
print(getData.Test) -- true


-- Saving
local sampleData = {
    Money = 12,
    Test = true
}
local Encoded = HttpService:JSONEncode(sampleData) --This will become string
gameDS:SetAsync("key_001", Encoded) -- saved
2 Likes

Are you using a plugin to save or just a normal script?

Im just using a normal script. I used the plugin to check if my datastore was working

1 Like

Are you trying to create a type checking variable? The script will think that myloaded and table are two seperate variables. It is the same as making a new line between the two. Add a colon after the myloaded part or just remove the “table” part of the script (or even connect myloaded and table).

local myloaded: table = DataStore:GetAsync(plr.UserId.."ThingsIAdded")

What he says is probably the easier way to do something like this. You can code yourself two functions. One is decoding and one is encoding, to make things easier. If you want an example, say so.

1 Like

Sorry I wrote it wrong. Ofc its myloadedtable
Thx anyways🙃

Thank you so much! I’ve been trying for so long before and now it finally works!

1 Like