PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

GetAsync

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

key

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)
	end)
	if success then
		gold.Value = myGold
	else
		gold.Value = STARTING_GOLD
	end

	leaderstats.Parent = player
end

for _, player in pairs(Players:GetPlayers()) do
	onPlayerAdded(player)
end
Players.PlayerAdded:Connect(onPlayerAdded)