Saving Player Data

Submitted by FireBlaze on Sat, 06/15/2019 - 19:48

Roblox allows data to be saved on its servers. The primary purpose of this feature is to store player data between sessions, keeping their stats, inventory, and other data intact. In this tutorial, we’ll create a system that automatically stores a player’s money and experience so that it can be retrieved when they play the game again.

Creating a Data Module

Before we dive into data storage, let’s set up the system which creates a leaderboard and values which can be used as the currency. We’ll start by creating a Script, an ideal place for this Script is within ServerScriptService.

Start by typing in:

____________________________________________________________

game.Player.PlayerAdded:Connect(function(player) -- might want to reference to the Arguments and Parameters article

      local leaderstats = Instance.new("Folder")

     leaderstats.Parent = player

     local cash = Instance.new("NumberValue",leaderstats)

     cash.Value = 10 -- This is our currency

end)

___________________________________________________________

The above code should create a leaderboard, if not, please double and triple check that you have the correct code. 

Enabling API Services

Make sure to save your game and for this purpose, publish the game. After publishing, press Create on the Roblox Homepage. This should bring you to a tab called "My Creations". Press on the Cog/System button on the right of the edit icon. Then, click Configure Game. Then, tick the box that says: "Enable Studio Access to API Services:". Save. 

Making Data-Stores

Now let’s start storing actual information using data stores.

To start, we must save the players data to an unique value inside the player, so that we can retrieve it when the player enters the game. We do this because, we can't just save it onto one server, because the server could shut down, or the player could join a different server. We must find an unique value inside a player, this value must be only one value in the eternity of Roblox. One option is the player name. But since the player can change the name, this option is out. The UserId of an player is unchangeable and there is only one UserId specified for the player. Every UserId is different. Perfect! Now that we have our value, in the same script in the ServerScriptService, write: 

_______________________________________________________

local DataStoreService = game:GetService("DataStoreService")

local cashData = DataStoreService:GetDataStore("CashData") -- You can name this to anything.

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

      local leaderstats = Instance.new("Folder")

     leaderstats.Parent = player

     local cash = Instance.new("NumberValue",leaderstats)

     cash.Name = "Cash"

     local data

     local success = pcall(function()

            data = cashData:GetAsync(player.UserId.."-cash") -- make it unique

    end)

    if success then

            cash.Value = data

    else

            print("There was an error when getting your data")

            print("Are you a new player?")

    end

end)

game.Players.PlayerRemoving:Connect(function(player)

   local success, errormessage = pcall(function()

           cashData:SetAsync(player.UserId.."-essence",player.leaderstats.Cash.Value)

   end)

 

   if success then

          print("Data saved")

   else

          warn(errormessage)

   end

end)

________________________________________________________________________

Understanding the Code

In this code, instead of immediately giving the player 10 cash, we check for any signs of Cash saved into the player's UserId. If there is, we get the data, using: GetAsync(). This is checked using the if success statement. If it's not success, then we say that data is not found and ask if the player is a new player. If the player leaves, we set the current data in the game session onto the player UserId, overwriting or making an new cash variable, so that when the player joins again, it'll save. The SetAsyc() command has two arguments: player.UserId.."-cash" and player.leaderstats.Cash.Value. The first one is the location of the data and the second one is the value of the data. If one argument is missing, it will not save., breaking the code. Fortunately, if it does break, for it didn't get the data, the pcall function Is there to go on even if there is an error.

Making an Changing Value

Add this code to the last line in the PlayersAdded Function:

____________________________

cash.Value = cash.Value + 1

___________________________

Test the game in Roblox. (Not Roblox Studio).

Exit the game.

Re-enter the game.

Did the Cash save?

If so, well done! You are on your way to a great Roblox Coder!

If not, please check the above codes and make sure you have gotten it correct.