In this tutorial we will see how to make the best DataStoreService system I know.
For this tutorial we will use two services which are the DataStoreService and the HttpService
To do this, we will therefore call these two services using two variables.
local DataStore = game:GetService("DataStoreService")
local HttpService = game:GetService("HttpService")
One of the problems with roblox databases are the limits given to players.
They can be seen here:
Server Limits
For example, instead of saving the coins value each time it is modified, we will save it when the player disconnects, so that many requests are not made unnecessarily.
We will also retrieve the player’s information when he connects.
local DataStore = game:GetService("DataStoreService")
local HttpService = game:GetService("HttpService")
game.Players.PlayerAdded:Connect(function(player)
end)
game.Players.PlayerRemoving:Connect(function(player)
end)
The best way to save player data is to use lists. These allow you to store a lot of information in just a single request. The problem of not using a list and that each data represents a request
We will first create a variable to retrieve the table containing all the player data and we will also retrieve the data of a player.
To retrieve the data of a player we will use are UserId followed by a text representing the data that we retrieve, this avoids using the same Key.
! As you probably know it is impossible to save a list with the DataStoreService these are why we are going to use the HttpService to convert the list into text or convert a text into a list.
local DataStore = game:GetService("DataStoreService")
local HttpService = game:GetService("HttpService")
local PlayerTable = DataStore:GetDataStore("PlayerData")
game.Players.PlayerAdded:Connect(function(player)
local GetData = PlayerTable:GetAsync(player.UserId.."Data")
end)
game.Players.PlayerRemoving:Connect(function(player)
end)
Now that we have retrieved the player data we will check if it exists.
If it does not exist:
We will create a list representing the default data of the players.
If this data exists:
We will convert the text to a list.
local DataStore = game:GetService("DataStoreService")
local HttpService = game:GetService("HttpService")
local PlayerTable = DataStore:GetDataStore("PlayerData")
game.Players.PlayerAdded:Connect(function(player)
local GetData = PlayerTable:GetAsync(player.UserId.."Data")
if GetData == nil then
GetData = {} -- We transform the variable into a list
GetData.Coins = 0 -- We establish the default values of the list
GetData.Level = 1
GetData.Rank = "Noob"
GetData.XP = 0
else
GetData = HttpService:JSONDecode(GetData) -- We turn the text into a list
end
end)
game.Players.PlayerRemoving:Connect(function(player)
end)
After retrieving the player’s information, we will have to store it somewhere to be able to modify it or retrieve it.
Of course, since the code is on the Server side, the players will not be able to modify these values.
To save them we will create a folder inside the player named PlayerInfo. Inside this folder there will be a list of player information.
local DataStore = game:GetService("DataStoreService")
local HttpService = game:GetService("HttpService")
local PlayerTable = DataStore:GetDataStore("PlayerData")
game.Players.PlayerAdded:Connect(function(player)
local GetData = PlayerTable:GetAsync(player.UserId.."Data")
if GetData == nil then
GetData = {} -- We transform the variable into a list
GetData.Coins = 0 -- We establish the default values of the list
GetData.Level = 1
GetData.Rank = "Noob"
GetData.XP = 0
else
GetData = HttpService:JSONDecode(GetData) -- We turn the text into a list
end
local Fold = Instance.new("Folder")
Fold.Parent = player
Fold.Name = "PlayerInfo"
for name, value in pairs(GetData) do
local NewInstance = nil
if tonumber(value) ~= nil then
NewInstance = Instance.new("NumberValue")
else
NewInstance = Instance.new("StringValue")
end
NewInstance.Parent = Fold
NewInstance.Value = value
NewInstance.Name = name
end
end)
game.Players.PlayerRemoving:Connect(function(player)
end)
After making a system to automatically create the data we need, we ended up retrieving player information. Now we only have to save the player data.
local DataStore = game:GetService("DataStoreService")
local HttpService = game:GetService("HttpService")
local PlayerTable = DataStore:GetDataStore("PlayerData")
game.Players.PlayerRemoving:Connect(function(player)
local ListData = player.PlayerInfo:GetChildren()
local DataList = {}
for i = 1, #ListData do
local Info = ListInfo[i]
DataList[Info.Name] = Info.Value
end
PlayerTable:SetAsync(player.UserId.."Data", HttpService:JSONEncode(DataList))
end)
Of course, this tutorial is an example to show you how useful lists can be. It is possible to integrate lists into lists for advanced developers.
Thanks to the use of lists in the DataStoreService I was able to create a save system for a level editor.
Thank you for reading this tutorial right to the end! I would modify it according to your opinions or your requests.