Basic Data Store Tutorial

This tutorial will teach you how to save data!

by LadyCelastia

Author Avatar

Introduction

A working Data Saving System is an essential part of a good Roblox game. As Data Saving becomes more and more of a standard across all Roblox games, your game cannot get popular without it now.

Basics

DataStoreService allows you to store data for Players or even Cross-Server. Since DataStoreService is a top-class Service, you can only get it via :GetService(). Here's how you can do it.

local DataStoreService = game:GetService("DataStoreService")

And now, you've created a pathway to DataStoreService. But you're not done yet. You need to specify the DataStore that you want to modify. To do that, you have to use :GetDataStore().

local DesiredDataStore = DataStoreService:GetDataStore("You can make your own name for your DataStore here!")

In the next few sections, we'll teach you how to modify a DataStore.

Setting Data

Now you've got the path to your desired DataStore. It's time to modify it. The most basic of all is setting new data. Any update on any DataStore will make network calls which might fail sometime. It is best to wrap them in a pcall() function to prevent the script from breaking. A key is required to modify the specific data you desired. Imagine if all of the data don't have a key, you won't be able to find out which data is the data you wanted. With keys, you can get data much easier. The format of keys is variant from scripters to scripters. It is the best to practice and find out which format fits you the most. Here's an example to show how you can get data from DataStoreService.

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("ExampleDataStore")

game.Players.PlayerAdded:Connect(function(Player)

    local Key = Player.UserId.. "-FirstTimePlayed"

    DataStore:SetAsync(Key, true)
end)

The code above sets the value "-FirstTimePlayed" in "ExampleDataStore" to true. You can also set strings, numbers and even tables as the value!

Getting Data

You've stored data on the DataStore now! But how can you get them back from the DataStore?

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("ExampleDataStore")

game.Players.PlayerAdded:Connect(function(Player)

    local success, errorMessage = pcall(function()

        local Key = Player.UserId.. "-ExampleKey"

        local Data = DataStore:GetAsync(Key)

        if Data then

            print("The player has data! " ..Data)

        else print("The player don't have any data!")

        end

    end)

    if not success then

        warn("DataStoreService Error! " ..errorMessage)

    end
end)

The code above gets the value "-ExampleKey" from the DataStore "ExampleDataStore" with a key. It is most likely that the player is the first time playing the game if the game already has an automatic saving system.

Updating Data

UpdateAsync is an alternate method to update the value of an existing data inside a DataStore. Here's an example on the usage of UpdateAsync.

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("ExampleDataStore")

game.Players.PlayerAdded:Connect(function(Player)
    local success, errorMessage = pcall(function()
        local Key = Player.UserId.. "-ExampleKey"
        local dataExists = DataStore:GetAsync(Key)
        if dataExists == nil then
            DataStore:SetAsync(Key, false)
        else
            return DataStore:UpdateAsync(Key, true)
        end
    end)
    if not success then
        warn("DataStoreService Error! " ..errorMessage)
    end
end)

This concludes the basics of DataStoreService, thanks for reading my tutorial!

View in-game to comment, award, and more!