How to Save Player Data in Roblox (DataStore Script That Doesn’t Lose Data)

How to Save Player Data in Roblox (DataStore Script That Doesn’t Lose Data)

Updated November 2026 · 6 min read

If players lose their coins or stage when they rejoin, they don't come back. Saving data in Roblox uses DataStoreService, and it's easy to get almost right. Here's a script that does it safely, and the mistakes to avoid.

Before you start

  • DataStores only work in a published place. Publish it once (File → Publish to Roblox).
  • To test in Studio, open Game Settings → Security and turn on Enable Studio Access to API Services. Studio then reads and writes your real data, so be careful.
  • Saving happens on the server, in a Script in ServerScriptService. A LocalScript can't use DataStores.

The script

Put this in a Script in ServerScriptService:

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")

local store = DataStoreService:GetDataStore("PlayerData_v1")
local DEFAULT = { Coins = 0, Stage = 1 }
local sessionData = {}

local function load(player)
	local ok, data = pcall(function()
		return store:GetAsync("player_" .. player.UserId)
	end)
	if not ok then
		-- Don't start them on empty data: saving it later would wipe their real data.
		player:Kick("Couldn't load your data. Please rejoin.")
		return
	end
	sessionData[player] = data or table.clone(DEFAULT)
end

local function save(player)
	local data = sessionData[player]
	if not data then return end
	local ok, err = pcall(function()
		store:UpdateAsync("player_" .. player.UserId, function()
			return data
		end)
	end)
	if not ok then
		warn("Save failed for " .. player.Name .. ": " .. tostring(err))
	end
end

Players.PlayerAdded:Connect(load)
Players.PlayerRemoving:Connect(function(player)
	save(player)
	sessionData[player] = nil
end)

-- When the server shuts down, PlayerRemoving may not get to finish. Save everyone here too.
game:BindToClose(function()
	for _, player in Players:GetPlayers() do
		task.spawn(save, player)
	end
	task.wait(3)
end)

Other scripts change sessionData[player].Coins while the player is in the game; the script saves it when they leave. To show coins on the leaderboard, copy them into a leaderstats folder too.

The mistakes that lose data

  • No pcall. DataStore calls can fail when Roblox has a hiccup. Without pcall, the error stops your script and nothing saves.
  • Saving defaults after a failed load. If loading fails and you carry on with empty data, the save on leave overwrites their real progress. That's why the script kicks instead.
  • Saving every time a value changes. DataStores have request limits. Save on leave, on shutdown, and every few minutes at most, not on every coin.
  • No BindToClose. When a server closes, players can be removed before their save finishes.
  • Changing the store name. PlayerData_v1 is where the data lives. Rename it and everyone starts over.

The details and limits are in Roblox's data stores docs.

Let the AI write it, then check it

You can ask an AI connected to Studio (setup guide) for this:

Make a server-side save system with DataStoreService for Coins and Stage. Wrap every DataStore call in pcall, kick the player if loading fails instead of giving them default data, save on PlayerRemoving and in BindToClose, and don't save on every change. Then playtest, rejoin, and check the values came back.

Then check its script against the list above. "DataStore calls without error handling" is one of the bugs AI code makes most in Roblox, and Larpings checks every script edit for it. See what else it checks.