NOTE: Currently, I only know about Setting and Retrieving data. Therefore, I don't know everything about Data Stores. Sorry for the inconvenience.
Accessing Data Stores
We use "DataStoreService" to access Data Stores. for example:
DSS = game:GetService("DataStoreService")
DATA STORES MUST ONLY BE USED IN SERVER SCRIPTS.
What Are Data Stores?
Imagine a drawer, as a player. The Drawer would have a lot of files. Thise Files would be called the Data Stores. In those files, are 2 columns. A Key and a Value. You use a Key to gain/set data. There is a limit, please read the wiki for more info.
Getting Data Stores
First, you have to get the data store, before you can do anything else. We use :GetDataStore(name) to do that.
If the Data Store hasn't been created yet, ROBLOX will automatically create a new one.
Getting Values
In this case, we use :GetAsync(key). This gives us the value of the key in the parenthesis. Example:
print(DSS:GetDataStore(player.Name.."'s Random"):GetAsync(Random))--Output will print whatever the values are for that key.
If there is no key/value, it will return nil
Setting Values
In order to get values, you need to set one. To set a NEW value, you would use :SetAsync(key,value).
Example:
DSS:GetDataStore(player.Name.."'s Random"):SetAsync("LOL", true)
Example Script
This script will save the player's Cash if it has not been saved before.
player = game.Players.Player1
DSS = game:GetService("DataStoreService")
PlayerStore = DSS:GetDataSotre(player.Name.."'s DataStores")
if PlayerStore:GetAsync(player.Name.."'s stats") == nil then
PlayerStore:SetAsync(player.Name.."'s stats",player.leaderstats.Cash.Value)
end
2 comments
So what's the difference between a key and a value? Why do you keep doing the player.Name.."'s Random" thing? What does the LOL and true do for SetAsync? The wiki uses UpdateAsync instead, is that better? Thanks.
Keys are Strings and values can be anything. the VALUE can change but the KEY CANT. The Key is basically a code to identify a value. UpdateAsync is used to update a Key's value. It is strongly suggested to use UpdateAsync if you know that the Key already exists. LOL and true are just examples. LOL is the key, and true is the value.