Hello! With Datastore2, there is actually a really simple way to save data with tables. Just look at this script:
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local ServerScriptService = game:GetService("ServerScriptService")
local DataStore2 = require(ServerScriptService:WaitForChild("DataStore2"))
DataStore2.Combine("DATA-Test","MainDataStore-Test")
local defaultTab = {
AverageTypingSpeed = 0;
PersonalBestTypingSpeed = 0;
PersonalBestScore = 0
}
Players.PlayerAdded:Connect(function(player)
local MainDataStore = DataStore2("MainDataStore-Test",player)
local Folder = Instance.new("Folder"); Folder.Name = "Values"; Folder.Parent = player
local ATS = Instance.new("IntValue"); ATS.Name = "AverageTypingSpeed"; ATS.Value = 0;
local PBTS = Instance.new("IntValue"); PBTS.Name = "PersonalBestTypingSpeed"; PBTS.Value = 0;
local TypingSpeed = Instance.new("IntValue"); TypingSpeed.Name = "TypingSpeed"; TypingSpeed.Value = 0;
local Score = Instance.new("IntValue"); Score.Name = "Score"; Score.Value = 0;
local PBScore = Instance.new("IntValue"); PBScore.Name = "PersonalBestScore"; PBScore.Value = 0;
local function updateDataStore(updatedValue)
MainDataStore:SetBackup(5)
print("IsBackup:", MainDataStore:IsBackup())
local tab = MainDataStore:Get(updatedValue)
ATS.Value = tab["AverageTypingSpeed"]
PBTS.Value = tab["PersonalBestTypingSpeed"]
PBScore.Value = tab["PersonalBestScore"]
end
updateDataStore(defaultTab)
MainDataStore:OnUpdate(updateDataStore)
ATS.Parent = Folder
PBTS.Parent = Folder
TypingSpeed.Parent = Folder
Score.Parent = Folder
PBScore.Parent = Folder
end)
What's happening here? Well when the function "updateDataStore" is ran, it will first tell DataStore2 that it can try to get the table 5 times before it "pretends" there is no table. (See Here) If they successfully retrieve the table, you can then assign each value that you wanted to be saved to a value in the table.
Example.Value = exampleTab["Example"]
Now, to update/save the table, you can't actually use the increment function for this. You will have to use the Set function instead. I believe this is the only way you can save tables? At least to my knowledge. If you do find another function that could save tables then lmk :)
local newTable = {
AverageTypingSpeed = 15;
PersonalBestTypingSpeed = 5;
PersonalBestScore = 12
}
MainDataStore:Set(newTable)
This should work and hopefully you can use this for whatever project your working on :) Have a great day!
2 comments
Datastore can't save different types of values. Make sure all are strings or integers. Unless they changed this.
Since you're using DataStore2, ask on this thread: https://devforum.roblox.com/t/how-to-use-datastore2-data-store-caching-and-data-loss-prevention/136317