Give your game a real memory with DataStoreService, so a player's coins survive the game closing.
Right now, everything you've built dies when the game stops. Close the test and your coins are gone. Real games remember you. That memory lives in a DataStore — and this lesson is where your game gets a brain that survives restart.
DataStores only fully work in published games. If you test from Studio, saves won't stick around — that's expected, not a bug. You'll test the full loop once we publish.
Saving is always the same dance:
local DataStoreService = game:GetService("DataStoreService")
local coinsStore = DataStoreService:GetDataStore("Coins")
GetDataStore("Coins") gives you a filing cabinet named "Coins". Inside it, each key is a player. So "Sam's file" = key "Sam", value 50.
What does GetDataStore("Coins") give you?
Two methods do almost all the work:
coinsStore:SetAsync(player.UserId, coins) -- write a value
local coins = coinsStore:GetAsync(player.UserId) -- read it back
SetAsync(key, value) — "set asynchronously": write the value to the store. Roblox does the slow network work in the background so your game doesn't freeze.GetAsync(key) — returns the saved value, or nil if there's no save yet.Notice the key is player.UserId — the permanent number from last lesson. The key is who; the value is what.
In coinsStore:SetAsync(player.UserId, coins), the key is and the value is
Load on join, save on leave. That's the entire shape of persistence:
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) or 0
-- ...build leaderstats with coins...
end)
Players.PlayerRemoving:Connect(function(player)
local coins = player.leaderstats.Coins.Value
coinsStore:SetAsync(player.UserId, coins)
end)
Every game with player progress — cash, levels, pets — is this pattern with different values.
A player is leaving. Write the line that saves their coins (held in player.leaderstats.Coins.Value) to the store under their UserId.
Players.PlayerRemoving:Connect(function(player)
-- your save line
end)GetAsync gives nil. Default it with or 0.Next: loading a player's data in detail — and handling brand-new players.