If this function throws an error, the error message will describe the problem. A full list of DataStore errors can be seen here.
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)
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:
Data store keys must be strings no more than 50 characters long. Attempting to use longer strings will result in this error.
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.
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.