Welcome to this tutorial made by me !
DataStore are used in various games like Jailbreak or Adopt me to save players data ! Anyone who knows DataStore is definitely not a beginner and can be proud of himself.
To access DataStores you’ll need to enable Studio Access to Roblox Api :
Now the fun can start
local DataStoreService = game:GetService("DataStoreService") -- Access DataStoreService
local database = DataStoreService:GetDataStore("database") -- Create a new database if its unexistent otherwise access it
Since it’s a tutorial for dummies we’ll only cover two of the most important function :
:GetAsync()
and :SetAsync()
:GetAsync()
is used to get data from a database using a scope (like player’s user id)
:SetAsync()
is used to put data by taking two arguments : the scope (player user id, don’t use player’s username to save data as it can be changed for 1000 robux) and the data (numbers, table…).
Here’s an illustrated example of what a coin datastore looks like:
Let’s now use our knowledge for example the coin data of a player leaderboard (I’m assuming you know how to make a leaderboard).
local DataStoreService = game:GetService("DataStoreService")
local coinDatabase = DataStoreService:GetDataStore("coinDatabase") -- Make the coin database
local players = game:GetService("Players")
-- If a player is added
players.PlayerAdded:Connect(function(player)
-- Make the leaderboard
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
-- Make the coin value Inside of the leaderboard
local coins = Instance.new("IntValue")
coins.Parent = player
-- Try getting the data from the database
local data = coinDatabase:GetAsync(player.UserId)
-- If data found then puts it in coins, otherwise uses default value
if data then
coins.Value = data
else
coins.Value = 30
else
end)
-- If the player is leaving
players.PlayerRemoving:Connect(function(player)
coinDatabase:SetAsync(player.UserId, player.leaderstats.Coins.Value)
end)
TADAAA, we got our server script ! It works most of the time. Now you’re probably asking : fancym what do you mean most of the time ? And yes , it will only work most of the time because the api is unreliable and can fail to load or save data. But we can maximize our chance of not getting this error with this technique :
Instead of just getting or saving the data we can make an attempt system that will keep trying until the script did 5 attempts or it successfully loaded or saved the data.
Like this :
local DataStoreService = game:GetService("DataStoreService")
local coinDatabase = DataStoreService:GetDataStore("coinDatabase") -- Make the coin database
local players = game:GetService("Players")
-- If a player is added
players.PlayerAdded:Connect(function(player)
-- Leaderboard part bla bla bla
-- Keep trying to get the data until 5 attempts or success
local attempt = 0
repeat
local success, error = pcall(function()
local data = coinDatabase:GetAsync(player.UserId)
if data then
player.leaderstats.Coins.Value = data
else
player.leaderstats.Coins.Value = 30
end
end)
attempt += 1
task.wait()
until success or attempt == 5
end)
-- If the player is leaving
players.PlayerRemoving:Connect(function(player)
-- Keep trying to save the data until 5 attempts or success
local attempt = 0
repeat
local success, error = pcall(function()
coinDatabase:SetAsync(player.UserId, player.leaderstats.Coins.Value)
end)
attempt += 1
task.wait()
until success or attempt == 5
end)
There you go ! Now you can save data as much as you want ! You’re now a pro : )