DataManager - A module solely dedicated for Data Management

Hello everyone. We all know that data management may be complicated over time, especially if you’re dealing with a high player base. For my projects, I’ve always used the same module that I’ve been improving over time, and today I felt like sharing it so you guys may be able to use it (or customize) as you wish.

The module consists into:

  • Using both regular and ordered datastores to handle data saving;
  • Making data management easier, giving you the opportunity to add, edit (or delete) stats as you wish;
  • Providing helpful functions so it can be used literally anywhere.

To start off, the module uses a key for each player (their UserId) - it also contains a retry function and also auto saves data within the given time. There should be indications in the module itself. The module also creates a remote function inside ReplicatedStorage called ‘GetPlayerData’ that allows the client to grab their data at any time.

As I said, the module uses both regular and ordered datastores, but what does that mean? Here’s the ‘trick’:

local DATASTORE_KEY = "Data:%d"

local DataStoreKey = DATASTORE_KEY:format(Player.UserId)
local DataStore = DataStoreService:GetDataStore(DataStoreKey)
local OrderedDataStore = DataStoreService:GetOrderedDataStore(DataStoreKey)

The example above gets both a regular and an ordered datastore using a key. The thing is, the ordered datastore saves a unique key (os.time) at every time it does its saving process, then I can just grab the current page and attempt to grab data from the regular datastore using that unique key. Example:

local Key = os.time()
DataStore:SetAsync(Key, Data)
OrderedDataStore:SetAsync(Key, Key)

As you may see, I save a copy of the player’s data using that key, and I do also save a copy in the ordered datastore. That way, I can simply load the data by grabbing the most recent key from the ordered datastore. The module also contains a function that if it’s the first time that a player joined the game, it puts a default data so the player may proceed. See below:

local Default = GetDefaultData() -- can be found in the module
local Data = nil
local MostRecentKeyPage = Retry(function()
	return OrderedDataStore:GetSortedAsync(false, 1):GetCurrentPage()[1] -- Gets the most recent page containing the most recent 'key'
end).Data

if MostRecentKeyPage then
	local Request = Retry(function()
		return DataStore:GetAsync(MostRecentKeyPage.value) -- Attempts to grab data from the most recent key
	end)
	if Request.Success then
		Data = Request.Data
	else
		Player:Kick("Couldn't load your data! Please rejoin.")
		return
	end
else
	Data = Default -- If it's their first time joining or if there ain't any data, then attributes the game's default data
end

MODULE FUNCTIONS

  1. ResetData - Self-explanatory. Resets the data for the target player to the module’s default data;
  2. PlayerLoaded - Can be used to identify if the player’s data has been loaded;
  3. GetOfflineData - Can be used to get data from a player even if they’re not inside the game;
  4. SaveOfflineData - Can be used to save data for a player that isn’t inside the game;
  5. GetData - Returns a table containing the player’s data (must be inside the game);
  6. ChangeStat - Increments to an existing stat (coins, etc.);
  7. SetStat - Completely changes the value of a specific stat;

The module also comes with auto-saving features (even for shutdowns). Everything at the beginning of the module is pretty much customizable (as well as the module itself). Use, customize it for your game/projects as you feel fit; there should be some comments within the module, but I’ll be open to questions or suggestions.

Ah also, for the fact that this module also uses ordered datastores, it is possible to grab old data in case anything happens inside your game, I might come up with this function myself if y’all feel like it’s so needed.

:link: OBTAIN THE MODULE HERE: DataService - Roblox

3 Likes