Forja

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

  1. 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.
  2. 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.
  3. 3Press Play, change your coins, stop and play again: they're still there.
SaveData · a Script in ServerScriptService
-- 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

  • GetAsync reads the player's saved number when they join, and SetAsync writes it when they leave.
  • pcall catches a failed read or write, which happens when Roblox's service is busy, so the script keeps running.
  • BindToClose saves everyone when a server shuts down, when PlayerRemoving may 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 }).
Want something more? The script generator can write it: “Save and load a player's coins, level and inventory with a DataStore, safely”

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