Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

How to use DataStores?

Asked by 8 years ago

So I've got a script which if you own a gamepass you're added to the BetaPlayer list, but what I'm trying to do is make is universal, aka DataStored. BUT I have absolutely no clue how to use them.

I have tried http://wiki.roblox.com/index.php?title=Data_store but to no avail.

local BetaPlayers = {}

local passId = 151727012

function isAuthenticated(player)
    return game:GetService("MarketplaceService"):PlayerOwnsAsset(player, passId)
end

game.Players.PlayerAdded:connect(function(plr)
    if isAuthenticated(plr) or plr.Name == "Player" or plr.Name =="Player1" then
        table.insert(BetaPlayers, plr.Name)
        print(plr.Name)
    end
end)

Would someone be able to answer a short explanation on how to use them?

1 answer

Log in to vote
9
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
8 years ago
Edited 6 years ago

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.

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.

game.Players.PlayerAdded:connect(function(Player)
    PlayerJoined_DataStore:SetAsync(tostring(Player.UserId), 1)
end)
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.

game.Players.PlayerAdded:connect(function(Player)
    local Joined = PlayerJoined_DataStore:GetAsync(tostring(Player.UserId))

    local Stats = Instance.new('Folder',Player)
    Stats.Name = 'leaderstats'
    local JoinStat = Instance.new('NumberValue',Stats)
    JoinStat.Name = 'Joined'

    if Joined ~= nil or Joined == 0 then
        JoinStat.Value = Joined
        PlayerJoined_DataStore:SetAsync(tostring(Player.UserId), Joined + 1) --Of course this will overwrite already saved data.
    else
        PlayerJoined_DataStore:SetAsync(tonumber(Player.UserId), 1)
    end
end)

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.

game.Players.PlayerAdded:connect(function(Player)
    Joined = PlayerJoined_DataStore:IncrementAsync(tostring(Player.UserId), 1) --This will automatically add 1 to the amount of times that player has joined.

    local Stats = Instance.new('Folder',Player)
    Stats.Name = 'leaderstats'
    local JoinStat = Instance.new('NumberValue',Stats)
    JoinStat.Name = 'Joined'
    JoinStat.Value = Joined --Since IncrmentAsync automatically returns the amount of the key it will go through all the checks. All you have to do is set the value.
end)

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.

game.Players.PlayerAdded:connect(function(Player)
    local Stats = Instance.new('Folder',Player)
    Stats.Name = 'leaderstats'
    local JoinStat = Instance.new('NumberValue',Stats)
    JoinStat.Name = 'Joined'
    local Joined = 0

    PlayerJoined_DataStore:UpdateAsync(tostring(Player.UserId), function(OldValue) --Like I said the second argument is a function, and the function will have a parameter of the old value.
       Joined = OldValue or 0 --What or is acting like in this case is an if then statement. If OldValue is nil then we'll want the better of it and use 0.
        Joined = OldValue + 1
        return Joined
    end)
end)

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.
Ad

Answer this question