Tutorial:Data Stores

This article is outdated and is no longer the correct way to use Datastores. View Roblox's article on Datastores for more information.

(By Kitrank Vopafal)

In this tutorial, I will demonstrate how to use data stores to save values for your game:

Firstly, insert a Script inside ServerScriptService. Then insert the following line:

We have to get the DataStoreService before we can call it. So we created a variable called DSS, short for DataStoreService, which is what we want, something short to reference, instead of having to say game:GetService("DataStoreService") every single time.

Next, we insert this block of code, which runs when the Player and Player's Character are added. Basically when someone first joins the game, it will run whatever code we put inside it and it will happen for every player.

We are creating a new Folder object, naming it Data (where we will store our data values), and placing it inside the Player. If you did everything correctly so far, your explorer should look like this:

Now we will instance/create an IntValue, call it Cash, set the default value to 0, and place it inside the Data folder.

Here's what your explorer should look like now:

Now we're going to go back near the top of the script under where we made the DSS variable and create the Data Store for our Cash:

We just created a Data Store, but we still need to make it load and save. You can change the variable PlayerCash and the "Cash" text inside the quotation marks. Now on to loading the Data Store whenever the Player joins the game:

Now where it sets our Cash value to 0 by default, we will make it load our Cash OR set it to 0 by default. If the player has no save, it will load what they had before, otherwise start at 0 (or whatever you prefer). Now on to saving:

First, we're going to add another function at the bottom (inside the player/character added function) which will automatically run right before the Player leaves the game and save our Cash. Then:

You successfully created a value to save, and made it save/load whenever the player joins/leaves. Congratulations! Additionally, if you want your data to save automatically every set interval, you can do this:

Replace the number inside the wait with the time you want (in seconds) in-between each auto save. Note: There are limits to how frequently you can make data store requests (link here for specific limitations), so do not auto save too fast.

To test the Data Store, test run the game in Stuido, then manually change the value of your Cash in your Player. Stop the test run after changing the value yourself, then test run again to see that it saved what you changed it to. That's it for this tutorial, hope it helped!