Loading Data v1 Review

Hello Roblox community,
Today I would like to present my code, which aims to analyze logical changes in player data. Unfortunately, I have adapted it to only one data architecture, which is a bit of a shame, but I plan to make it work with multiple architectures later. I would therefore like you to analyze the code and give me some advice !

--Class Data
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local DataStore = DataStoreService:GetDataStore("PlayerData")

local GameData = ReplicatedStorage.GameData

local GlobalPlayerData = require(GameData.GlobalPlayerData)

local PREFIX_DATA = "PlayerKeyData_"

local function refreshTableFromAnotherTable(refreshTable, referanceTable)
	if not referanceTable then return end
	
	for i, v in referanceTable do
		if refreshTable[i] and typeof(refreshTable[i]) == typeof(v) then continue end
		
		if v ~= "Value" then -- a remplacer par un systeme de key word
			if refreshTable[i] == v then continue end
		end
	
		refreshTable[i] = v
		print("Changed element", i, v)
	end
	
	return refreshTable
end

local function checkCurrentDataFromNew(data)
	assert(typeof(data) == "table", "failed element is not table or nil")
	local refreshData 
	
	refreshData = refreshTableFromAnotherTable(data, GlobalPlayerData.Data)
	
	for i, v in refreshData do
		local params = {}
		
		local newTable = refreshTableFromAnotherTable(v, GlobalPlayerData.Data[i], params)
		
		refreshData[i] = newTable
	end
	
	return refreshData
end

local function takeTheExistingData(player: Player)
	local key = PREFIX_DATA..tostring(player.UserId)
	local data

	local success, errorMessage = pcall(function()
		data = DataStore:GetAsync(key)
	end)
	
	assert(success, errorMessage)
	assert(data, "Data doesn't exist !")
	
	print("Data exist !", data)
	
	return data
end

local function checkIfTheDataAlreadyExist(player: Player)
	local key = PREFIX_DATA..tostring(player.UserId)
	local data
	
	local success, errorMessage = pcall(function()
		data = DataStore:GetAsync(key)
	end)
	
	assert(success, errorMessage)
	
	return (data)
end

local Data = {}
Data.__index = Data

function Data.new(player: Player)
	assert(player, "Player doesn't exist")
	
	local startDT = os.clock()
	local data
	
	if checkIfTheDataAlreadyExist(player) then
		data = takeTheExistingData(player)
		
		if not data then return end
		
		data = checkCurrentDataFromNew(data)
	end
	
	if not data then
		data = GlobalPlayerData.Data
	end
	
	local self = setmetatable({}, Data)
	
	self.player = player
	self.data = data
	
	return self, os.clock() - startDT
end

function Data:removing()
	local key = PREFIX_DATA..tostring(self.player.UserId)
	
	local success, errorMessage = pcall(function()
		DataStore:SetAsync(key, self.data)
	end)
	
	assert(success, errorMessage)
	
	setmetatable(self, nil)
	table.clear(self)
	
	print("Success data has been saved")
end

return Data

--GlobalData

local GlobalPlayerData = {}

GlobalPlayerData.Data = {
	{
		Name = "Coins",
		Value = 0,
	},
	
	{
		Name = "dddd",
		Value = 0,
	},
}

return GlobalPlayerData

Exemples:



ANDDDD

I was thinking about creating a keyword system here, but I’m not sure if it’s a good idea.

image

Good day !