API:Class/GlobalDataStore/GetAsync

GetAsync

YieldFunction of ExplorerImageIndex69.pngGlobalDataStore
There are members of the same name. See the disambiguation page.
Variant GetAsync (
    string key
)
Parameters:
  1. key

Returns: Variant
Description: Returns the value of the entry in the DataStore with the given key. If the key does not exist, returns nil. This function caches for about 4 seconds: you cannot be sure that this function returns the current value saved on the Roblox servers. Mind that there are limits that apply to this.


If this function throws an error, the error message will describe the problem. A full list of DataStore errors can be seen here.

Example

The below example would retrieve a player's saved "Gold" from a DataStore, and put the returned value onto the leaderboard.

local DataStore = game:GetService("DataStoreService"):GetDataStore("Gold") 
 
game.Players.PlayerAdded:connect(function(Player)
    local PlayerKey = "user_" .. Player.userId --// Never use the player's name as a key. 
 
    local leaderstats = Instance.new("IntValue", Player)
    leaderstats.Name = "leaderstats"
 
    local Gold = Instance.new("IntValue", leaderstats)
    Gold.Name = "Gold"
    Gold.Value = DataStore:GetAsync(PlayerKey) --// Retrieving the value of the saved key.
end)


Errors

This function has the potential to error. It is recommended to wrap this function in a pcall. Here are the potential errors that can be thrown:

Key too long

Data store keys must be strings no more than 50 characters long. Attempting to use longer strings will result in this error.

Request Limit

A game is limited in how many requests it can make across all of its servers. If this error occurs the game should be restructured to make fewer Data Store requests.

Web Error

All Data Store requests create web requests to perform the desired operation. If there is a connection issue or back-end problem, this function will throw a web error. In cases like this it is recommended to retry the request after a short delay.

See Also