How Can I Add Things To A DataStore Array?

So, I was wondering if it was possible for me to add things to a datastore array. I think I tried it before but it didn’t work.

local DSS = game:GetService("DataStoreService")
local DS = DSS:GetDataStore("Messages")

table.insert(DS:GetAsync("Main"), "Message")
1 Like

I would recommend that you make DS:GetAsync("Main") a variable. Also, I recommend using pcalls when doing stuff related to DataStores.

local DSS = game:GetService("DataStoreService")
local DS = DSS:GetDataStore("Messages")
local data = DS:GetAsync("Main")

table.insert(data, "Message")
1 Like

Try retrieving the data, put the data in a table, then use table.insert, then save the data again:

local DSS = game:GetService("DataStoreService")
local DS = DSS:GetDataStore("Messages")

local Data = {DS:GetAsync("Main")} -- Only add these brackets if you didn't save a table already

table.insert(Data, "Message")

DS:SetAsync("Main", Data)
2 Likes

Would I be able to use an if statement for if the Datastore = nil?

if Data == nil then
Data:SetAsync("Main", {})
end
1 Like

You would do DS:SetAsync("Main", {}) as data is just what the DataStore returned and not the DataStore itself.

2 Likes

So, that should work, correct?

1 Like

If you are referring to what I said, yes. I would also do Data = {} after doing :SetAsync to make the table not nil.