Back to blog
Sep 16, 2024
7 min read

Understanding Datasaving On Roblox

I will break down what data saving is and how we can use it effectively.

When players invest time and effort into your Roblox game, they expect their progress to be saved. Whether it’s currency they’ve earned, items they’ve collected, or achievements they’ve unlocked, players want assurance that their hard work won’t disappear. This is where datastores come in. In this post, we’ll dive into what datastores are, how they work, and how you can implement them to save and load player data in Roblox.


1. What Are Datastores?

Datastores are essentially Roblox’s method for saving player data between gaming sessions. Think of them as a database that stores important information—whether that’s how much money a player has, what items are in their inventory, or even their position on a leaderboard.

Without datastores, player progress would be wiped each time they leave the game. With them, players can pick up right where they left off, which is crucial for long-term retention and satisfaction.

Why Datastores Matter:

  • Persistence: Players can continue their progress across multiple sessions.
  • Customization: Allows you to save anything from in-game currency and inventory to complex stats like skill trees or achievements.
  • Player Retention: Games that save progress are more likely to keep players coming back.
  • Example: If you’re building a tycoon game, datastores will let players save their building progress and financial gains, so they can resume where they left off the next time they play.

2. How Datastores Work: A Basic Overview

At its core, a datastore is a table where you can store key-value pairs. The key is usually the player’s unique UserID, and the value is the data you want to store for that player. Roblox provides a DatastoreService that lets developers easily implement this system.

Here’s how the process generally works:

  • When the player joins the game: Retrieve their saved data from the datastore (if it exists).
  • During gameplay: Update their data in real-time (but without saving it constantly, to avoid overload).
  • When the player leaves: Save their updated data to the datastore for next time.

3. Setting Up a Basic Datastore

Let’s jump into some code to see how a basic datastore is set up.

  1. Accessing DatastoreService First, you need to access Roblox’s DatastoreService, which will allow you to create and manipulate data for your game.
-- ServerScriptService/DataStore.lua

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")                     -- Players service reference for later
local playerData = DataStoreService:GetDataStore("PlayerData") -- Create or access the datastore

Here, we create a datastore called PlayerData. This will store information for each player.

  1. Loading Player Data When They Join When a player joins, we’ll want to load their data from the datastore. If it’s their first time, they won’t have any saved data yet, so we’ll initialize a default set of values.
-- ServerScriptService/DataStore.lua

Players.PlayerAdded:Connect(function(player)

    -- Wrap the get in a pcal for security
    local success, data = pcall(playerData.GetAsync, playerData, player.UserId)
    
    -- Check if the value was returned
    if success then
        if data then
            print("Loaded data for player: ", player.Name)
            -- Load player stats (like currency or inventory)
            player.leaderstats.Money.Value = data.Money or 0
            player.Inventory = data.Inventory or {}
        else
            print("No data found, creating default data for player: ", player.Name)
            -- Set default values for new players
            player.leaderstats.Money.Value = 0
            player.Inventory = {}
        end
    else
        warn("Could not load data for player: ", player.Name)
    end
end)

Here’s what’s happening:

GetAsync(): We attempt to fetch the data for the player using their UserId as the key. If the player has no saved data, we initialize some default values, such as starting money or an empty inventory. The pcall() function ensures that any potential errors (such as network issues) don’t crash the game.

  1. Saving Player Data When They Leave Once the player leaves, you’ll want to save their data back into the datastore. Let’s see how to do that:
-- ServerScriptService/DataStore.lua

game.Players.PlayerRemoving:Connect(function(player)
    -- Put all player data into a dictionary
    local playerDataTable = {
        Money = player.leaderstats.Money.Value,
        Inventory = player.Inventory
    }

    -- Request player data to be saved
    local success, err = pcall(playerData.SetAsync, playerData, player.UserId, playerDatatable)

    if success then
        print("Successfully saved data for player: ", player.Name)
    else
        warn("Failed to save data for player: ", player.Name, "Error: ", err)
    end
end)

Here’s what’s happening:

SetAsync(): This saves the player’s current data (money, inventory, etc.) into the datastore. Again, we use pcall() to prevent any issues from causing the game to crash.

  1. Best Practices for Datastores While setting up datastores is relatively straightforward, there are some important best practices to follow:
  • Save Data Sparingly Roblox imposes limits on how often you can read and write to datastores. Save data only when necessary (such as when a player leaves), and avoid saving data continuously throughout gameplay.

  • Handle Errors Gracefully Network issues, server crashes, or Roblox outages can sometimes prevent data from saving. Make sure to use pcall() to catch these errors and notify players if something goes wrong. It’s always a good idea to have a backup system or retry mechanism for critical data.

  • Limit Data Size Each key in the datastore has a size limit, and storing too much data can cause problems. Avoid storing unnecessary data (like large arrays) and compress your data if possible. Consider splitting data into multiple keys if you need to store a lot of information.

  • Use OrderedDatastores for Leaderboards If your game includes a leaderboard, use OrderedDatastores to store player rankings based on specific values (like total coins collected). This allows you to easily retrieve the top players.

  1. Common Mistakes to Avoid While working with datastores, there are a few common pitfalls developers should be aware of:
  • Not Testing Thoroughly Make sure to test your datastore implementation thoroughly—especially when dealing with large player bases. Check for edge cases, such as what happens when a player’s data fails to load or save.

  • Overloading the Datastore Saving too often can overload Roblox’s datastore service, leading to throttling or failures. Keep in mind that you have limited read/write operations per minute.

  • Data Loss Due to Server Crashes Occasionally, servers may crash before data is saved properly. Consider implementing autosave systems that back up critical player data at intervals during gameplay (every few minutes).


Datastores are a critical part of any Roblox game that requires persistent player data. Whether you’re building a tycoon, an RPG, or any game that involves progression, implementing datastores correctly will ensure that players can pick up right where they left off.

By following best practices like handling errors, saving data sparingly, and properly testing your code, you can create a reliable experience that keeps players engaged and invested in your game.

Now that you have a good understanding of datastores, start implementing them in your next Roblox project and give your players the seamless experience they deserve!