How can i improve my datastore?

  • What does the code do and what are you not satisfied with?
    There is nothing with it, as far as I know, the only thing I could improve is the GetAsync part.

  • What potential improvements have you considered?
    Improving the GetAsync part, but haven’t started yet.

  • How (specifically) do you want to improve the code?
    I want to know how I could improve my Datastore below, Perfectionize so the datastore can easily hold 40+ Values with a minimal of 12 players in a Server.

local players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")
local saveDataStore = dataStoreService:GetDataStore("DataStore11")

local StatTable = require(game.ReplicatedStorage.Functions.StatTable)

players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	
	for i, v in ipairs(StatTable.Stats) do
		local stat = Instance.new("StringValue")
		stat.Name = v
		stat.Parent = leaderstats
	end
	
	local DataId = Instance.new("IntValue")
	DataId.Value = 0
	DataId.Parent = plr
	local data = saveDataStore:GetAsync(plr.UserId)
	if data then
		for name,value in pairs(data.leaderstats) do	
			leaderstats[name].Value = value
		end
	end
end)

local function serializePlayerData(plr)
	local saveData = {leaderstats = {} }
	for _, stat in pairs(plr.leaderstats:GetChildren()) do
		saveData.leaderstats[stat.Name] = stat.Value
	end
	return saveData
end
		
local function savePlayerData(plr)
	local saveData = serializePlayerData(plr)
	saveDataStore:UpdateAsync(plr.UserId, function(oldValue)
		local PreviousData = oldValue or {DataId = 0}
			if saveData.DataId == PreviousData.DataId then
				saveData.DataId = saveData.DataId + 1
			return saveData
		else
			return nil
		end
	end)
end
	
local RunService = game:GetService("RunService")
if not RunService:IsStudio() then
	game:BindToClose(function()
		local plr = game.Players:GetPlayers()
		local n = 0
		for _, Player in pairs(plr) do
			task.spawn(function()
				pcall(savePlayerData, Player)
				n += 1
			end)
		end
		while n < #plr do task.wait(1) end
	end)
end
2 Likes

You should implement retrying, basically:

local successful
local result
local retries = 0
repeat
    successful, result = pcall(save, player) -- or get
    if result then
        return result
    end
    retries += 1
until successful or retries > MAX_RETRIES

Sooo… Something like this right?

	local Successfull
	local Result
	local Retries = 0
	local MAX_RETRIES = 3
	
	repeat
		Successfull, Result = pcall(function()
			local Data = saveDataStore:GetAsync(plr.UserId)
			for name, value in pairs(Data.leaderstats) do
				leaderstats[name].Value = value
				if Result then
					return Result
				end
				Retries += 1
			end
		end)
	until Successfull or Retries > MAX_RETRIES
	if Retries == MAX_RETRIES then
		plr:Kick("Failed to load data. Please rejoin the game.")
	end
end)
local Successful
	local Result
	local Retries = 0
	local MAX_RETRIES = 3
	repeat
		Successful, Result = pcall(saveDataStore.GetAsync, saveDataStore, plr.UserId)
		Retries += 1
	until Successfull or Retries > MAX_RETRIES
	if Retries > MAX_RETRIES then
		plr:Kick("Failed to load data. Please rejoin the game.")
	elseif Successful then
		for name,value in pairs(Result.leaderstats) do	
			leaderstats[name].Value = value
		end
	end
end)

Isn’t this better? If not, Why?

	local Successfull
	local Result
	local Retries = 0
	local MAX_RETRIES = 3
	
	repeat
		Successfull, Result = pcall(function()
			local Data = saveDataStore:GetAsync(plr.UserId)
			for name, value in pairs(Data.leaderstats) do
				leaderstats[name].Value = value
				if Result then
					return Result
				end
				Retries += 1
			end
		end)
	until Successfull or Retries > MAX_RETRIES
	if Retries == MAX_RETRIES then
		plr:Kick("Failed to load data. Please rejoin the game.")
	end
end)

I see my own mistake now… Sorry

I tried it some days after, but it doesn’t really like, index nil with leaderstats.