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

How does DataStore work and how to use it?

Asked by
StoIid 364 Moderation Voter
8 years ago

All I know is that DataStore is where you store data to roblox servers and it's pretty much better than Data Persistence. I am looking to find out how to use it.

1 answer

Log in to vote
2
Answered by
sigve10 94
8 years ago

Basically, DataStorage (DS) is a newer way of storing information through servers, and is referred to as an upgraded version of Data Persistence (DP)

How to use: Let's say that you have a value that you call "Points" in your Script (which will store data for a player). It would basically look like this:

Points = 10

Now, let's say that a player joins, and you want this player to get this value stored in the DataStorage. This is what you would need to start with:

local DataStore = game:GetService("DataStoreService"):GetDataStore("Points")

In the line above here, you can see us defining DataStore, which is easily reached using GetService. In the end of this line, you see us using a command: "GetDataStore". This command retrieves a DataStore, which in this case would be Points. If you are saving more dataStores, make sure that they don't have the same names. Now, we want a player to get a point from for example clicking a button:

local DataStore = game:GetService("DataStoreService"):GetDataStore("Points")
local Button = workspace.Button.ClickDetector

Button.MouseButton1Click:connect(function(Player)
    local key = "user_" .. Player.userId
    DataStore:UpdateAsync(key, function(EarlierValue)
        local newValue = EarlierValue or 0 --In case the player doesn't have a Value
        newValue = newValue + 1
        return newValue
    end)
end)

In the script above, you can see us SAVING a value. Yes, that's right, saving! How did we do that? Well, as you can see, we use a basic button-clicking function for checking when a button is clicked. We then, assign a key. This key is really important, as it makes sure that the value is saved for that ONE SPECIFIC PLAYER! The reason we're using the User ID is because of the name changing system of ROBLOX, which could lead to a loss of data if the player changed their name. We then use a function on the DataStore, known as: "UpdateAsync". This function updates the earlier value of the dataStore. We then define a new Value, which is either the earlier value, or a new value. We then add a point to the player, and at last, we return the value so that it can be saved. But wait, what if we want to use this data in a leaderboard? We would then use a basic function known as GetAsync. This function will retrieve the old value, and is used inside of variables:

game.Players.PlayerAdded:connect(function(player)
    key = "user_" .. player.userId
    local Data = DataStore:GetAsync(key)
    --Do whatever you want to do with your data here.
end)

I hope this helped!

0
Where would this type of script go? In a regular script I'm assuming. Also would it go in starterpack, or workspace? StoIid 364 — 8y
0
This is a serverscript, so it would need to be in a server area, which could be replicatedStorage, workspace, serverStorage, etc. sigve10 94 — 8y
0
Ah, ok. Will the script create datavalues under a player that can be edited by other scripts if i were to implement them? StoIid 364 — 8y
Ad

Answer this question