Roblox save data script
Keeps a player's coins between visits with a DataStore. It doesn't save over someone's coins when their save couldn't be read.
Where it goes
- 1Publish your game, then in Studio open File, Game Settings, Security and turn on Enable Studio Access to API Services. Without it, saving doesn't work in Studio.
- 2In the Explorer, hover over ServerScriptService, press + and choose Script, and paste this in. It already makes the Coins stat, so you don't need the leaderboard script as well.
- 3Press Play, change your coins, stop and play again: they're still there.
-- From forja.us/tools/roblox-scripts/save-data-- Saves each player's Coins, and loads them when they come back.local DataStoreService = game:GetService("DataStoreService")local Players = game:GetService("Players")local store = DataStoreService:GetDataStore("PlayerCoins")-- Who loaded fine: a failed load must never save over their coins.local loaded = {}local function onPlayerAdded(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 then coins.Value = saved or 0 loaded[player] = true else warn("Could not load coins for " .. player.Name .. ": " .. tostring(saved)) endendlocal function save(player) if not loaded[player] then return end local leaderstats = player:FindFirstChild("leaderstats") local coins = leaderstats and leaderstats:FindFirstChild("Coins") if not coins then return end local ok, problem = pcall(function() store:SetAsync("player_" .. player.UserId, coins.Value) end) if not ok then warn("Could not save coins for " .. player.Name .. ": " .. tostring(problem)) endendPlayers.PlayerAdded:Connect(onPlayerAdded)for _, player in Players:GetPlayers() do task.spawn(onPlayerAdded, player)endPlayers.PlayerRemoving:Connect(function(player) save(player) loaded[player] = nilend)-- The server is closing: save everyone still here.game:BindToClose(function() for _, player in Players:GetPlayers() do save(player) endend)How it works
GetAsyncreads the player's saved number when they join, andSetAsyncwrites it when they leave.pcallcatches a failed read or write, which happens when Roblox's service is busy, so the script keeps running.BindToClosesaves everyone when a server shuts down, whenPlayerRemovingmay not get the chance.
Make it yours
- Save more than coins
- Save a table instead of a number:
store:SetAsync(key, { Coins = coins.Value, Wins = wins.Value }).
Maps to try it on: simulator and tycoon maps
Start from a finished map, and your scripts have a game to run in.
More free Roblox scripts
Roblox kill brick scriptRoblox leaderboard scriptRoblox sprint scriptRoblox double jump scriptRoblox teleport scriptRoblox obby checkpoint scriptRoblox coin pickup scriptRoblox day and night cycle scriptRoblox game pass scriptRoblox developer product scriptRoblox door scriptRoblox tool giver scriptRoblox round system scriptRoblox background music scriptRoblox speed pad scriptRoblox jump pad scriptRoblox VIP door scriptRoblox daily reward scriptRoblox NPC dialogue scriptRoblox pet follow scriptRoblox gun scriptRoblox admin commands scriptRoblox tycoon dropper scriptRoblox shop GUI script

