First, you probably have to understand how DataStores work.
DataStores basically work like a dictionary. They need a key to access the data.
For instance, it might look like this inside of the DataStore
{1512612612 = 500}
Basically the first value is the key and the second one is the value that can be accessed with the key (just like dictionaries work)
Here is an easy example:
local DataStoreService = game:GetService("DataStoreService")
local MyFirstDataStore = DataStoreService:GetDataStore("MyFirstDataStore")
Alright, here we just make two variables. The first one defines the DataStoreService which is a service, just like ReplicatedStorage, Workspace, ServerStorage, … .
Then, we call a method on our DataStoreService variable which is called GetDataStore.
GetDataStore basically creates gets the DataStore with the name in the quotation marks, if there is no DataStore with that name yet, it creates one.
Alright, so we assigned our new DataStore with the name MyFirstDataStore to our MyFirstDataStore variable.
Now we actually want to save Data.
I will try to make this as easy as possible for you.
Alright, so for now we are going to give your friend who is called Ben 500 cash when he joins the game.
In our case, Ben is our key (don’t do that please, use the UserId instead as it’s more unique, Ben could change his name for example and his data would be lost).
We want the dictionary to look like this
{["Ben"] = 500}
So we do
local DataStoreService = game:GetService("DataStoreService")
local MyFirstDataStore = DataStoreService:GetDataStore("MyFirstDataStore")
MyFirstDataStore:SetAsync("Ben", 500)
Alright. Now we call a method on our MyFirstDataStore variable which is SetAsync.
SetAsync basically creates a new entry into our DataStore dictionary. The first parameter is the key and the second parameter is the actual value.
Our key is “Ben” and our value is 500.
Alright. Now we set our DataStore entry but we have to check everytimes a player joins if his name is Ben and if so, give him 500 coins.
Let’s do that!
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local MyFirstDataStore = DataStoreService:GetDataStore("MyFirstDataStore")
MyFirstDataStore:SetAsync("Ben", 500)
Players.PlayerAdded:Connect(function(playerThatJoined)
--create leaderstats
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = playerThatJoined
local cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Parent = leaderstats
--now check if the player that joined is your friend, Ben
local async = MyFirstDataStore:GetAsync(playerThatJoined.Name) --This will become 500 (our value for Ben) if it's actually Ben. If not, it will be nil
if async ~= nil then --Check if it's not nil
--it's not nil. It's Ben!
cash.Value = async --async is 500 (our value for Ben)
end
end)
Alright. Now you know the basics of getting data and setting data.
Remember that DataStores are like dictionaries, they have a key and a value that is assigned to that key.
When doing DataStore:GetAsync(playerName), then it will either become the value of the key that you entered as parameter or nil.
You can store strings, numbers, tables, … inside of DataStores! They are very useful.
If you still don’t understand something, I would recommend you Youtube. Also you might want to try the DevHub, even though I found it a little worse than Youtube.
Here a script that saves when someone leaves and gets their value when someone joins:
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local MyFirstDataStore = DataStoreService:GetDataStore("MyFirstDataStore")
Players.PlayerAdded:Connect(function(playerThatJoined)
--create leaderstats
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = playerThatJoined
local cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Parent = leaderstats
local async = MyFirstDataStore:GetAsync(playerThatJoined.UserId)
if async ~= nil then --Check if it's not nil
cash.Value = async
end
end)
Players.PlayerRemoving:Connect(function(playerThatLeaves)
MyFirstDataStore:SetAsync(playerThatLeaves.UserId, playerThatLeaves.leaderstats.Cash.Value)
end)
You would also want to pcall the SetAsyncs, but you will eventually find out yourself.