Skip to content

Roblox DataStore Script

Copy a free datastore example, learn where it goes, then describe your own version.

Updated Oct 2026BeginnerExample · not runtime verified

Example: “Save player coins with DataStore”

ScriptServerScriptService
Luau syntax (not run)Placement rules (not run)Sandbox startup (not run)Roblox (not run)
DataStore.server.lua
-- Minimal save example: test in a separate published experiencelocal Players = game:GetService("Players")local DataStoreService = game:GetService("DataStoreService")local store = DataStoreService:GetDataStore("Coins_v1")local loaded = {}Players.PlayerAdded:Connect(function(player)    local ok, value = pcall(function()        return store:GetAsync(tostring(player.UserId))    end)    if not ok then player:Kick("Could not load data. Please rejoin.") return end    player:SetAttribute("Coins", tonumber(value) or 0)    loaded[player.UserId] = trueend)local function save(player)    if not loaded[player.UserId] then return end    local ok, err = pcall(function()        store:UpdateAsync(tostring(player.UserId), function()            return player:GetAttribute("Coins") or 0        end)    end)    if not ok then warn("Save failed", err) endendPlayers.PlayerRemoving:Connect(function(player)    save(player)    loaded[player.UserId] = nilend)game:BindToClose(function()    for _, player in Players:GetPlayers() do save(player) endend)

Where to put it

  1. Open your own experience in Roblox Studio and show the Explorer and Output panels.
  2. Published test experience with API access. This learning example needs retries, autosave and session locking before production.
  3. Insert a Script at ServerScriptService.
  4. Paste the code, press Play and test the behavior. Read Output if anything fails.

Line by line

1. Loads a coin value when the player joins.

2. Avoids saving a default value if loading fails.

3. Attempts to save on leaving and server shutdown.

Common mistakes

!
Check the object names and script location.

Names are case sensitive. A LocalScript needs a client execution context, while server data and permissions belong in a Script. Test with multiple players before publishing.

DataStore script FAQ

Does this work in any Roblox game?
Use it in an experience you own or have permission to edit in Roblox Studio. It is not an external script injector.
Has this example been verified in Roblox?
No. This preview includes source examples and placement guides. Generated customizations show their actual check results, but Roblox execution is not connected. Test your copy in Studio.
Can I customize the example?
Yes. Copy or download the code and adjust it in Studio. AI customization is available through the generator, with check results shown for each generated script.

More script types