Save progress (DataStore)
So far the coins are gone as soon as a player leaves the game. With the DataStore, Roblox remembers the progress – for the next visit too.
What you’ll learn
- open a DataStore
- save when leaving and load when joining
- catch errors with pcall
Preparation
DataStores only work in a published game. To test in Studio, switch on Enable Studio Access to API Services under Game Settings → Security.
Load and save
Extend your leaderstats script from lesson 9. As the key you use the player’s UserId – a number that never changes. pcall catches errors when Roblox is not responding, so your script doesn’t crash.
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local store = DataStoreService:GetDataStore("Progress")
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Parent = leaderstats
local ok, saved = pcall(function()
return store:GetAsync("Player_" .. player.UserId)
end)
if ok and saved then
coins.Value = saved
end
end)
Players.PlayerRemoving:Connect(function(player)
local value = player.leaderstats.Coins.Value
pcall(function()
store:SetAsync("Player_" .. player.UserId, value)
end)
end)Good rules
Only save game data (coins, stage, unlocked items) – never personal data like names or age. Don’t save every second, but when leaving and at most every few minutes, because Roblox limits the requests.
🛠️ Try it
Save the coins from lesson 9. Test: start the game, collect coins, stop, start again – are they still there?
🧠 Mini quiz
Which key is best for saving?
The UserId never changes – names can change.
What is pcall for?
If the DataStore doesn’t respond, pcall prevents a crash.
✓ Lesson done! On to the next one.
Roblox and Roblox Studio are trademarks of the Roblox Corporation. mahshid.de is not affiliated with Roblox. All links to Roblox lead to the official sites.