Pull a player's save on join, default brand-new players to zero, and hand the number to leaderstats.
Saving is half the loop; loading is the half players actually see. This lesson wires GetAsync into PlayerAdded so a returning player gets their coins back — and a brand-new player starts at zero.
Inside PlayerAdded, pull the player's file:
local DataStoreService = game:GetService("DataStoreService")
local coinsStore = DataStoreService:GetDataStore("Coins")
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
local coins = coinsStore:GetAsync(player.UserId)
print(player.Name .. " has " .. coins .. " coins saved")
end)
GetAsync(key) returns the saved value. For a returning player that's their real coin count — the number you stored when they last left.
GetAsync returns nil when…
A brand-new player has no save, so GetAsync gives nil. Before doing anything with the number, decide what a missing save means:
local coins = coinsStore:GetAsync(player.UserId)
if coins == nil then
coins = 0
end
The shorthand does the same thing in one line:
local coins = coinsStore:GetAsync(player.UserId) or 0
or 0 reads: "give me the saved value, or if that's nil, give me 0." It's the standard "default for new players" line in every Roblox game.
local coins = coinsStore:GetAsync(id) or — what default goes here for a brand-new player?
Now the two patterns meet: the loaded value feeds straight into leaderstats.
Players.PlayerAdded:Connect(function(player)
local coins = coinsStore:GetAsync(player.UserId) or 0
local stats = Instance.new("Folder")
stats.Name = "leaderstats"
stats.Parent = player
local coinsValue = Instance.new("NumberValue")
coinsValue.Name = "Coins"
coinsValue.Value = coins -- <-- the loaded number
coinsValue.Parent = stats
end)
Returning player: the leaderboard shows their saved coins. New player: it shows 0. Same code, both cases — the or 0 did the deciding.
Inside PlayerAdded, load the player's coins from the store, defaulting to 0 if they have no save. Use the or shorthand.
Players.PlayerAdded:Connect(function(player)
-- load coins, defaulting new players to 0
end)coins is nil → coinsValue.Value = nil leaves the stat empty or errors. Always default it.Next: keeping saves safe with the full save loop and autosave.