Explain DataStoreService to me

I need help with datastoreservice because the explanations in the developer api suck if im being honest. So can anyone help explain DataStoreService clearly to me?

DataStoreService is pretty simple. Is a way to save players’ data. When a player first joins the game you would get their data using :GetAsync() and whenever you save their data you would use :SetAsync() that’s pretty much all there is. The only thing you will have to keep in mind is how often you save ones data since if you are constantly saving you will run into data queues fairly often and that is not a good thing since you want to prevent data loss and one way to do this to perfect the way to save data.

A script would look something like this:

local DataStoreService = game:GetService("DataStoreService") -- Get the DataStoreService

local Data_Key = "PlayerData" -- This is a key which all data will be saved under. Never change this
local DataStore = DataStoreService:GetDataStore(Data_Key) -- Gets the data from the given key
--!! It is important that you don't change the key. All data is saved under this key, changing it will give everyone new data instead of their saved data

local function LoadData(player: Player)
   local Success, Data = pcall(function() -- pcall is a Protected Call which means if this does error the script won't break, and there is a chance this could error so it's best to wrap it in a pcall
      return DataStore:GetAsync("PlayerData_"..player.UserId) -- Gets the players data which we are storing using their player.UserId since that will never change..
   end)

   if Success then
      print(Data) -- This is their data
   end
end

local function SaveData(player: Player)
   local Success, Error = pcall(function() 
      return DataStore:SetAsync("PlayerData_"..player.UserId, Data.To.Save) -- Data.To.Save would be the data your saving to the DataStore
   end)
   if not Success then print(Error) end -- If there is an error let us know
end

That is pretty much the basic run down of DataStoreService, I got all of this from the Developer Page on DataStores. Think of DataStores as a massive table so you have your DataStoreKey which would be a table and inside of that table would be the player keys and inside of that would be the player’s data:

PlayerData: DataStoreKey = {
    PlayerData_90351039 = {...}, -- A players Data
    PlayerData_1 = { -- This how the players data would look
       Cash = 50,
       -- etc
    }, 
    -- So on
}

Hope this helps, there are some limitations to how much you can save and how often but I would still recommend diving deeper since having good data management in your game is vital. Have a great day!

1 Like