Hey! So, I created my first custom data storing system and I’d like to get some feedback on it. Like I said, I’ve never made one before so I’m not sure if there is any glitches or faults with it.
Setup:
As you can see, it’s made up of 3 main scripts. The “DataStoreManager” will retrieve the player’s data upon joining, where it then uses “DataUtility” to update the Player’s data table (incase new additions were made to the default table). To use the player’s data, I just require the “DataCache” from another script. Once a player decides to leave, “DataStoreManager” retrieves the used cache from “DataCache” and sets it as the Player’s data.
I normally like to keep things simple so this is how I do it
local module = {}
local dataStoreService = game:GetService("DataStoreService")
local players = {}
game.Players.PlayerAdded:Connect(function(player)
local dataStore = dataStoreService:GetDataStore("Player", player.UserId)
local success, response = pcall(dataStore.GetAsync, dataStore, "Data")
if success == false then return end
players[player] = response or {}
end)
game.Players.PlayerRemoving:Connect(function(player)
if players[player] == nil then return end
local dataStore = dataStoreService:GetDataStore("Player", player.UserId)
local success, response = pcall(dataStore.SetAsync, dataStore, "Data", players[player])
players[player] = nil
end)
game:BindToClose(function()
while next(players) ~= nil do wait() end
end)
module.Get = function(player, key)
if players[player] == nil then return end
return players[player][key]
end
module.Set = function(player, key, value)
if players[player] == nil then return end
players[player][key] = value
end
return module