DataStoreService
Show Deprecated
DataStoreService exposes methods for getting GlobalDataStore and OrderedDataStore objects. Data stores can only be accessed by game servers, so you can only use DataStoreService within a Script or a ModuleScript that is used by a Script.
See Data stores for an in-depth guide on data structure, management, error handling, etc.
Code Samples
This code sample prints the request budget for all data store request types.
DataStore Budget
local DataStoreService = game:GetService("DataStoreService")
for _, enumItem in pairs(Enum.DataStoreRequestType:GetEnumItems()) do
print(enumItem.Name, DataStoreService:GetRequestBudgetForRequestType(enumItem))
end
Summary
Methods
Creates a DataStore instance with the provided name and scope.
Returns the default data store.
Get an OrderedDataStore given a name and optional scope.
Returns the number of requests that can be made by the given request type.
Returns a DataStoreListingPages object for enumerating through all of the experience's data stores.
Properties
AutomaticRetry
Methods
GetDataStore
Parameters
Returns
GetGlobalDataStore
Returns
Code Samples
Get GlobalDataStore Instance
local DataStoreService = game:GetService("DataStoreService")
local GlobalDataStore = DataStoreService:GetGlobalDataStore()
print(GlobalDataStore.Name)
GetOrderedDataStore
Parameters
Returns
Code Samples
OrderedDataStore Basics
local DataStoreService = game:GetService("DataStoreService")
local pointsStore = DataStoreService:GetOrderedDataStore("Points")
local function printTopTenPlayers()
local isAscending = false
local pageSize = 10
local pages = pointsStore:GetSortedAsync(isAscending, pageSize)
local topTen = pages:GetCurrentPage()
-- The data in 'topTen' is stored with the index being the index on the page
-- For each item, 'data.key' is the key in the OrderedDataStore and 'data.value' is the value
for rank, data in ipairs(topTen) do
local name = data.key
local points = data.value
print(name .. " is ranked #" .. rank .. " with " .. points .. "points")
end
-- Potentially load the next page...
--pages:AdvanceToNextPageAsync()
end
-- Create some data
pointsStore:SetAsync("Alex", 55)
pointsStore:SetAsync("Charley", 32)
pointsStore:SetAsync("Sydney", 68)
-- Display the top ten players
printTopTenPlayers()
GetRequestBudgetForRequestType
Parameters
Returns
Code Samples
Print Request Budget
local DataStoreService = game:GetService("DataStoreService")
local globalStore = DataStoreService:GetGlobalDataStore()
local function printBudget()
local budget = DataStoreService:GetRequestBudgetForRequestType(Enum.DataStoreRequestType.SetIncrementAsync)
print("Current set/increment budget:", budget)
end
for i = 1, 5 do
local key = "key" .. i
local success, err = pcall(function()
globalStore:SetAsync(key, true)
end)
if success then
printBudget()
else
print(err)
end
end