Datastores
Datastores are a way of accessing data and saving it, so players can continue their progress on a game. Datastores are(in my opinion) the most important thing in a game. Faulty datastores are something that can cause major problems in a game. Avoiding that is very important.
Let me explain some basic things.
• Roblox Datastores can fail at any time. There is nothing we can do about it. But we can try to prevent these to the furthest possibility.
• PCalls(Protected Calls) is a way of handling errors from code. It is recommended to only use these when accessing outside of Roblox.
• Try to save everything in one Datastore. It is a wise decision. There may be certain occasions that you MAY have to use more than one. But that is pretty uncommon.
• Datastores can be accessed anywhere inside of the game including all the places in the game.
I assume that you read those, so we can move on.
Using Tables
Tables are an important and very useful way to organize and store data. Let's start by creating our table to organize data.
local dataTable = {
Gold = 0;
Gems = 0;
}
As you can see, I made two different currency. One gold and one gems. I set both of those to "0".
Just keep this in your script and we will use it later.
Creating Datastores
To get a Datastore, we need to first get the DataStoreService by using ":GetService()".
local dataTable = {
Gold = 0;
Gems = 0;
}
local DataService = game:GetService("DataStoreService")
Next, let's get a Datastore by using ":GetDataStore()").
local dataTable = {
Gold = 0;
Gems = 0;
}
local DataService = game:GetService("DataStoreService")
local myDatastore = DataService:GetDataStore("ThisIsMyData123")
As you can see inside of the "()" for ":GetDataStore()", that is the name of your Datastore. You can name it to anything you want.
Now, let's create a simple leaderstats script. I assume you know how to create one.
local dataTable = {
Gold = 0;
Gems = 0;
}
local DataService = game:GetService("DataStoreService")
local myDatastore = DataService:GetDataStore("ThisIsMyData123")
game.Players.PlayerAdded:Connect(function(player)
local stats = Instance.new("Folder")
stats.Name = "leaderstats"
stats.Parent = player
local gold = Instance.new("IntValue")
gold.Name = "Gold"
gold.Value = 0
gold.Parent = stats
local gems = Instance.new("IntValue")
gems.Name = "Gems"
gems.Value = 0
gems.Parent = stats
end)
There we go. We made a simple leaderstats.
Next, we are going to check if the player joined has data. If the player does not, we will make new data for him using the dataTable. We will also use a method called ":GetAsync()". This will check if we have data. It also needs the key to access which is basically a unique code to access a player's data.
local dataTable = {
Gold = 0;
Gems = 0;
}
local DataService = game:GetService("DataStoreService")
local myDatastore = DataService:GetDataStore("ThisIsMyData123")
game.Players.PlayerAdded:Connect(function(player)
local stats = Instance.new("Folder")
stats.Name = "leaderstats"
stats.Parent = player
local gold = Instance.new("IntValue")
gold.Name = "Gold"
gold.Value = 0
gold.Parent = stats
local gems = Instance.new("IntValue")
gems.Name = "Gems"
gems.Value = 0
gems.Parent = stats
local checkData = myDatastore:GetAsync(player.UserId)
if checkData then
gems.Value = checkData.Gems
gold.Value = checkData.Gold
else
checkData = dataTable
myDatastore:SetAsync(player.UserId, checkData)
end
end)
Now, we have a basic data retriever. Let's work on the saving side now. We will be using the method ":SetASync()". The first parameter is the key that we said was the UserId. The second parameter is the Data we are saving.
local dataTable = {
Gold = 0;
Gems = 0;
}
local DataService = game:GetService("DataStoreService")
local myDatastore = DataService:GetDataStore("ThisIsMyData123")
game.Players.PlayerAdded:Connect(function(player)
local stats = Instance.new("Folder")
stats.Name = "leaderstats"
stats.Parent = player
local gold = Instance.new("IntValue")
gold.Name = "Gold"
gold.Value = 0
gold.Parent = stats
local gems = Instance.new("IntValue")
gems.Name = "Gems"
gems.Value = 0
gems.Parent = stats
local checkData = myDatastore:GetAsync(player.UserId)
if checkData then
gems.Value = checkData.Gems
gold.Value = checkData.Gold
else
checkData = dataTable
myDatastore:SetAsync(player.UserId, checkData)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
dataTable.Gold = player.leaderstats.Gold.Value
dataTable.Gems = player.leaderstats.Gems.Value
myDatastore:SetAsync(player.UserId, dataTable)
end)
Now, we have a basic working datastore. It saves and retrieves. But this isn't a good datastore. We need to use "PCalls" or Protected Calls like I said at the beginning because Roblox Datastores can fail. If they do, we do not want them to break our script. In the way we are using PCalls, It has two returns. Success and ErrorMessage. If the data was saved succesfully, it returns success. If not, it returns the Error Message.
local dataTable = {
Gold = 0;
Gems = 0;
}
local DataService = game:GetService("DataStoreService")
local myDatastore = DataService:GetDataStore("ThisIsMyData123")
game.Players.PlayerAdded:Connect(function(player)
local stats = Instance.new("Folder")
stats.Name = "leaderstats"
stats.Parent = player
local gold = Instance.new("IntValue")
gold.Name = "Gold"
gold.Value = 0
gold.Parent = stats
local gems = Instance.new("IntValue")
gems.Name = "Gems"
gems.Value = 0
gems.Parent = stats
local checkData
local success, errorMessage = pcall(function()
checkData = myDatastore:GetAsync(player.UserId)
end)
if checkData and success then
gems.Value = checkData.Gems
gold.Value = checkData.Gold
else
checkData = dataTable
local success, errorMessage = pcall(function() myDatastore:SetAsync(player.UserId, checkData)
end)
if success then
print("Data saved successfully")
else
print("Error occurred while saving Data.")
warn(errorMessage)
end
elseif not success then
print("Data error while loading")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
dataTable.Gold = player.leaderstats.Gold.Value
dataTable.Gems = player.leaderstats.Gems.Value
local success, errorMessage = pcall(function()
myDatastore:SetAsync(player.UserId, dataTable)
end)
if success then
print("Data saved successfully")
else
print("Error occurred while saving Data.")
warn(errorMessage)
end
end)
Now we have a really good and efficient Datastore. I hope this help anyone that is reading. I am not the best at explaining and writing things, but just try to understand haha. Thanks!
3 comments
nice
thanks alphexus, your a good friend. When im finished rewriting my code ima answer the question!
this is like a guide but a scriptinghelpers answer