GetAsync
This is a yielding function. When called, it will pause the Lua thread that called the function until a result is ready to be returned, without interrupting other scripts.
This function returns the value of the entry in the GlobalDataStore
with the given key. If the key does not exist, returns nil
. This function caches for about 4 seconds, so you cannot be sure that it returns the current value saved on the Roblox servers.
If this function throws an error, the Articles/Datastore Errors|error message
will describe the problem. Note that there are also Articles/Datastore Errors|limits
that apply to this function.
To save a data store entry, you can use one of several possible functions, including GlobalDataStore/SetAsync|SetAsync()
, GlobalDataStore/UpdateAsync|UpdateAsync()
, and GlobalDataStore/IncrementAsync|IncrementAsync()
.
Parameters
Name | Type | Default | Description |
---|---|---|---|
|
The key identifying the entry being retrieved from the data store |
Returns
Return Type | Summary |
---|---|
The value of the entry in the data store with the given key |
Code Samples
Data Store to Leaderboard
This code sample retrieves a player’s saved gold from a data store and puts the returned value onto the leaderboard. Note that this sample does not save players’ gold — it only loads it.
local Players = game:GetService("Players") local goldDataStore = game:GetService("DataStoreService"):GetDataStore("Gold") local STARTING_GOLD = 100 local function onPlayerAdded(player) local playerKey = "Player_" .. player.UserId local leaderstats = Instance.new("IntValue") leaderstats.Name = "leaderstats" local gold = Instance.new("IntValue", leaderstats) gold.Name = "Gold" local myGold local success, err = pcall(function() myGold = goldDataStore:GetAsync(playerKey) or STARTING_GOLD end) if success then gold.Value = myGold else -- Failed to retrieve data end leaderstats.Parent = player end for _, player in pairs(Players:GetPlayers()) do onPlayerAdded(player) end Players.PlayerAdded:Connect(onPlayerAdded)