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 =)
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