How to use DataStores
Its crucial to understand what datastores are and how they work. If one step is done incorrectly then all data could be lost.
There are three types of datastores, Normal, Ordered, and Global.
Global Datastores
These are basically 1 datastore which is accessable without specifying a datastore name. I myself barely ever use this so I won’t go into full detail about this one.
Ordered Datastores
This datastore is basically a number based datastore, allowing you only to save Number values to a key. This specific datastore type has bonus methods such as IncrementAsync, and GetSortedAsync.
This data store is most commonly used for leaderboards or count saving.
Normal Datastores
This is the most common datastore you’ll find and ever use. This datastore allows you to save the following for each key:
- Numbers
- Strings
- Booleans
- Tables (either full array or full dictionary, cannot be mixed)
- Nil
Saving Data
There are two different ways to save data on datastores. SetAsync, Which is slightly slower but gets the job done, and UpdateAsync which gives you better control over your data.
Getting the datastore:
This is how you get all datastores :
local DataStoreService = game:GetService("DataStoreService")
local DataStoreName = "PanelData" --| Replace with datastore name
local DataStore = DataStoreService:GetDataStore(DataStoreName) --| Normal
local DataStore = DataStoreService:GetOrderedDataStore(DataStoreName) --| Ordered
local DataStore = DataStoreService:GetGlobalDataStore() --| Global
Saving on the datastore:
This method saves data straight to the key.
local Key = "Warnings" --| All values will convert to string
local Data = { Warns = 0, UserId = 1 } --| Data doesn't have to be a table.
DataStore:SetAsync(Key, Data) --| Key (Name of data being saved), Data (What you're saving)
Updating on the datastore:
I prefer using this method for saving data, for it grants you the ability to check the current data and apply that to the new data without requiring to get the data first.
local Key = "Warnings"
DataStore:UpdateAsync(Key, function(pastData)
local currentWarnings = pastData.Warns --| Can read past/current data
local currentUserId = pastData.UserId --| Can read past/current data
local newData = { --| The new data (Doesn't have to be a table)
Warns = 0,
UserId = 1,
}
return newData --| Return nil to cancel or a value to replace the current data.
end)
Getting data from a key:
local Key = "Warnings"
local Data = DataStore:GetAsync(Key) --| Gets the data
The text above explains what and how you use datastores. Try applying them to your panel, especially the normal datastores. Just keep in mind that there is a limit on how many requests for both saving and getting at a time and will drop any further requests if the queue is full.