How can I save tables to datastore

  1. I want a script example for saving tables

  2. I checked YouTube, de Forum and chatgpt but I can’t find an answer

Once I need to save my tables to my data with a player id key and I need an example to get those player datas.

ProfileService is a datastore that can save an entire hierarchy of tables.

Normally you can’t store tables directly to DataStores, but by formatting it to JSON format will allow you to turn tables into strings, which DataStore can work with.

local HS = game:GetService("HttpService")
local DSS = game:GetService("DataStoreService")

local DataStore = DSS:GetGlobalDataStore()
local myData = {
   foo = 123;
   bar = "abc";
}

-- SAVING DATA
local stringData = HS:JSONEncode(myData)
DataStore:SetAsync("Userdata",stringData)

-- LOADING DATA
local raw = DataStore:GetAsync("Userdata")
local mySavedData = HS:JSONDecode(raw)

print(mySavedData.foo) --> 123

However be wary that JSON only allows you to store either arrays or dictionaries, you can’t use both of them at once.

Thanks buddy for this I’ll use this.

Converting to a string is not needed as the method automatically does that. You can just save a table directly

How can I do it brother? If you’ve got an example I’d see it.

The method :SetAsync() from Datastores already encode your data before actually sending it.
Which means you can store tables and dictionaries without encoding it yourself.

Oh yeah? I thought we could only store strings and values.

Huh, i thought it couldn’t store tables before, was there an update that allowed this?

You could always store tables in data stores; I’ve done it for years

(cc @ubermemoli)