Introduction
DataStores are an efficient way to load and store data for all places in a single universe. They can also be a pain in the neck to use at times. Some restrictions include a Data limit such as you can only use 60 + (10 * Players) GetAsync and SetAsync requests per minute (hopefully you would not need to use a SetAsync request everytime a person got a kill). There is even a 262660 character limit to the amount of data that can be saved which may make serialization of instances all the more difficult. However if you're just using numbers and boolean values you should never reach this limit.
However DataStores are much more reliable for saving and loading data than Data Persistence. They are also widely used because the information can be pulled from any server and the information is saved to the game and not the player.
Setup
So first off, I would recommend setting up a variable. This way every time you're trying to get a hold of your selected DataStore, you do not run the risk of a typo.
What you will need to do is get the DataStoreService and call the function :GetDataStore()
. With the function you'll want to put in the name you want to call that DataStore, it can be any name just make sure it will be one you'll remember or you'll find later.
1 | local PlayerJoined_DataStore = game:GetService( 'DataStoreService' ):GetDataStore( 'JoinedTimes' ) |
For my example, I will be tracking the amount of times a player has joined my game. For the scripts to follow, just imagine the variable exists for all of them.
Functions
SetAsync
SetAsync requires two arguments, the key and the value. Since my example is trying to find how many times a player has joined the game we would want to save the key as their name or UserId. For this example, I'll use UserId. We will also want to save the amount of times they've joined the game. For the time we'll just put 1 as the value. You will understand why when you keep reading. But for now, the example will be below.
1 | game.Players.PlayerAdded:connect( function (Player) |
2 | PlayerJoined_DataStore:SetAsync( tostring (Player.UserId), 1 ) |
Just a note, using SetAsync on a key that already exists will overwrite the value saved to it.
GetAsync
With this YieldFunction the script will hold up until it gets the information. We would be able to use that information to complete our desired result. All we will need for this function is the key we used. Since the key is the Player's UserId it should be easy to get. For my example, we'll just pretend we're making a leaderstats of how many times a player has joined the game. We will also be adding on to the amount of times the player has joined in this example.
01 | game.Players.PlayerAdded:connect( function (Player) |
02 | local Joined = PlayerJoined_DataStore:GetAsync( tostring (Player.UserId)) |
04 | local Stats = Instance.new( 'Folder' ,Player) |
05 | Stats.Name = 'leaderstats' |
06 | local JoinStat = Instance.new( 'NumberValue' ,Stats) |
07 | JoinStat.Name = 'Joined' |
09 | if Joined ~ = nil or Joined = = 0 then |
10 | JoinStat.Value = Joined |
11 | PlayerJoined_DataStore:SetAsync( tostring (Player.UserId), Joined + 1 ) |
13 | PlayerJoined_DataStore:SetAsync( tonumber (Player.UserId), 1 ) |
So what the script is doing is it will get the GetAsync value and set it to a variable. The script will then check that variable to make sure it's not inexistent and not 0. The script will then add on to the value with SetAsync and change the value of the leaderstat.
IncrementAsync
Now of course, there is an easier way to do this. Roblox would not want us to deal with all this code. IncrementAsync follows the same request limit as GetAsync and SetAsync, as long as you're using DataStores responsibly you should be fine. So for the example we'll shorten down the code to follow IncrementAsync. The two arguments required for this is the key and the increment or number value. IncrementAsync will even return the value of the key.
1 | game.Players.PlayerAdded:connect( function (Player) |
2 | Joined = PlayerJoined_DataStore:IncrementAsync( tostring (Player.UserId), 1 ) |
4 | local Stats = Instance.new( 'Folder' ,Player) |
5 | Stats.Name = 'leaderstats' |
6 | local JoinStat = Instance.new( 'NumberValue' ,Stats) |
7 | JoinStat.Name = 'Joined' |
8 | JoinStat.Value = Joined |
UpdateAsync
I have never touched this function, but from my understanding there are two arguments. A key of course and a function. The function can not wait at all so we can not use YieldFunctions or waits in it. What happens is the UpdateAsync function will get the old saved value, if an old saved value does not exist it will return nil. So, we'll just have the function return and add on to the amount of times the player has joined the game.
01 | game.Players.PlayerAdded:connect( function (Player) |
02 | local Stats = Instance.new( 'Folder' ,Player) |
03 | Stats.Name = 'leaderstats' |
04 | local JoinStat = Instance.new( 'NumberValue' ,Stats) |
05 | JoinStat.Name = 'Joined' |
08 | PlayerJoined_DataStore:UpdateAsync( tostring (Player.UserId), function (OldValue) |
09 | Joined = OldValue or 0 |
Hopefully this answered some questions you might have had about DataStores, please do not get me into working with GetSortedAsync. The function might have to be for another question and I have not worked all that much with it. If this helped you out or answered your question, do not forget to hit the upvote button and hit the accept answer button. Have any questions feel free to post them in the comments below.