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

How do DataStores work?

Asked by
PredNova 130
8 years ago

Hi!

I've been wondering about DataStores. I've read the wiki, watched tutorial videos off of youtube and read through some free model DataStore scripts but I still don't understand exactly how they work...

I get you need to make a DataStore and then generate each player a unique key, and the best way to do this is using their player id, like so;

local ds = game:GetService("DataStoreService"):GetDataStore("instances")
local key = "player-"..player.userId -- Yes I am aware this wouldn't work as player isn't defined ;)

But anything after this is just a huge what, especially when having many different instance values you wish to save... If someone could give me an explanation or a link to a useful video / website that'd be much appreciated!

Thanks =)

2 answers

Log in to vote
0
Answered by 8 years ago

Hi there!

I've also had trouble with Data Stores in the past (I still do!) but once you learn how it works it gets very easy! Note that you can save alot of things in Data Stores such as bool values, it doesn't always have to be an int value. Also another misconception about Data Stores is that you have to have a default ROBLOX leaderboard to make it work. Also remember that you can access Data Stores in any server script! Anyway here are a few methods :

:GetAsync

GetAsync is used to find the value of the saved Data Store. Here is an example of me using :GetAsync

local ds = game:GetService('DataStoreService'):GetDataStore('Anything')

game.Players.PlayerAdded:connect(function(player)
local coins = ds:GetAsync('User_' .. player.UserId .. ':Anything') -- GetAsync's arguments are : 'PlayerId' (refered to in the wiki as 'Key'). Also I've used ':NameOfDataStore', so keep that in mind! 
end)

Now we'll move on to :SetAsync

local ds = game:GetService('DataStoreService'):GetDataStore('Anything')

game.Players.PlayerAdded:connect(function(player)
local coins = ds:GetAsync('User_' .. player.UserId .. ':Anything')
coins:SetAsync('User_' .. player.UserId .. ':Anything', +100) -- There are 2 arguments for :SetAsync(key, value you want to change by)
end)

:UpdateAsync and :IncrementAsync are a bit tricky so I won't be explaining it here. Hope this helped and be sure to comment any mistakes in the code (I think the +100 was a mistake). Please remember that you can access Data Stores in any server script. Also instead of making a leaderboard, why not make a Gui that outputs how much Coins you have? If you want to do this then do :

if coins then
RandomGui.Text = tostring(coins) -- Change Random Gui to your gui
end
0
Thanks, this did indeed help! But say I were to make a table including all of the instance values I wished to save and connected it to a changed function, would this save all of the values in the table once one of them changes or would it not work at all?? PredNova 130 — 8y
0
I'm not sure but I know that you can save Keys as long as they are strings and that if you were to save a table of numbers they would have to go up in increments of 1. If the answer helped please don't hesitate to set this question as 'answered'. LifeInDevelopment 364 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

Answer this question