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 — and this lesson is where your game gets a brain that survives restart.
only fully work in published games — but before any store call works at all, you must enable API access in Game Settings → Security:
Without the right one enabled, every SetAsync/GetAsync fails silently with an access error and your saves never happen. Flip the checkbox first — it's the most common reason "saves don't work," and it's never the code.
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 . 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 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: safe calls with — because the network always finds a way to fail.