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

Can you save a table in DataStoreService?

Asked by 3 years ago

The title says it all.

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

When saving a table into a DataStore, it does not change the way of saving it, meaning that you should write the same things when you save any other value(although, if the table contains an instance, the script will not save the table)

I'll give you a little example of how to use it.

-- this script inserts the name of a table(string) which it's saved in a datastore with the game id when the player leaves.

local MyStore = game:GetService('DataStoreService'):GetDataStore('MyDataStore')
local tab = {}

local Plrs = game:GetService('Players')

Plrs.PlayerAdded:Connect(function(plr)
 local data
 local s, e = pcall(function()
  data = MyStore:GetAsync(game.GameId)
 end)
 if s then
  if data ~= nil then
    tab = data
    table.insert(tab, plr.Name)
  else
    table.insert(tab, plr.Name)
  end
 else
  warn(e)
 end
 print(tab)
end)

Plrs.PlayerRemoving:Connect(function()
 local s, e = pcall(function()
  MyStore:SetAsync(game.GameId, tab)
 end)
 if not s then 
  warn(e)
 end
end)

0
im kinda new to datastoreservice and scripting overall so thats a bit lengthly to understand right now (and i deducted some errors for what i have to do so i figured out a new plan) but thanks! i can use this as a future reference if i ever wanna put a table in it SuperSM1 67 — 3y
0
sorry if it had any errors, I couldn't see them because I made the code inside the website Sarturex857 109 — 3y
Ad

Answer this question