Scripting Book/Chapter 10

From Legacy Roblox Wiki
Jump to navigationJump to search

Introduction

Now we're going to take a huge step and learn about Data Persistence. I will try my best to explain this.

What is Data Persistence?

Data Persistence is used to save data to a player, so when they leave the game and come back another time, you can load the data.

What is it used in?

Data Persistence is used in 'Perilous Skies' by Crazyman32 to save your points and in Danedude34's longest obby in ROBLOX place.

What we're going to do

In this tutorial we will create a function that is fired when a player enters. We will use the 'WaitForDataReady()' function to wait for the player who just entered to connect to ROBLOX data-sharing.

Step 1

First we're going to create a 'onPlayerEntered' function that is fired when a player enters the server.

function onPlayerEntered(newPlayer)

Step 2

Now we need to put in the WaitForDataReady() function to wait for the player to connect to ROBLOX Data-Sharing:

function onPlayerEntered(newPlayer)
    newPlayer:WaitForDataReady()

Step 3

Let's say we have a leaderboard in the game that holds points and we want to load the new player's points that were previously saved. In order to load the points we need to use the 'LoadNumber( , )' function.

function onPlayerEntered(newPlayer)
    newPlayer:WaitForDataReady()
    newPlayer.leaderstats.Points.Value = newPlayer:LoadNumber("Points")

Step 4

Finally we need to end the function.

function onPlayerEntered(newPlayer)
    newPlayer:WaitForDataReady()
    newPlayer.leaderstats.Points.Value = newPlayer:LoadNumber("Points")
end

Next

Next we need to make a function that is fired when the player leaves.

Step 1

First we need to make the function

function onPlayerLeave(oldPlayer)

Step 2

Now we need to save the player's points using the 'SaveNumber( , )' function and we need the end to end the function.

function onPlayerLeave(oldPlayer)
    oldPlayer:SaveNumber("Points", oldPlayer.leaderstats.Points.Value)
end

Connecting the functions

We have the functions and now we need the connections.

Connection for onPlayerEntered

game.Players.PlayerAdded:connect(onPlayerEntered)

Connection for onPlayerLeave

game.Players.PlayerRemoving:connect(onPlayerLeave)

Final Script

function onPlayerEntered(newPlayer)
    newPlayer:WaitForDataReady()
    newPlayer.leaderstats.Points.Value = newPlayer:LoadNumber("Points")
end

function onPlayerLeave(oldPlayer)
    oldPlayer:SaveNumber("Points", oldPlayer.leaderstats.Points.Value)
end

game.Players.PlayerAdded:connect(onPlayerEntered)
game.Players.PlayerRemoving:connect(onPlayerLeave)

Things to know

In Data Persistence you can save more than just numbers, here's a list of all the Data Persistence functions you can use,

:SaveNumber(key, value)
:SaveBoolean(key, value)
:SaveInstance(key, value)
:SaveString(key, value)
:LoadNumber(key)
:LoadBoolean(key)
:LoadInstance(key)
:LoadString(key)


See also

Go to previous chapter or Continue to Chapter 11