Use key, not value to order data stores

Currently ordered data stores are sorted based on the value rather than the key. This means it is impossible to store any ordered data directly in an ordered data store, you can only store IDs that reference other data because data store keys are limited to 50 characters

My use case is for an in-game library where players can upload their creations. I want to make a UI where players can see the creations that are most downloaded, newest uploaded, top rated, etc

Currently I use OrderedDataStores with the ID of the user uploaded creation as the key, then the value is either the timestamp it was uploaded, the download count or thumbs up count. I then use GetSortedAsync to get the listing of creations then I have to use a GetAsync request for each result returned to get the actual data.

If I could just use the key to sort the datastore, then have more space to store more metadata about the user uploaded creation it would be much more efficient

local DataStoreService = game:GetService("DataStoreService")
local CreationsDataStore = DataStoreService:GetDataStore("Creations")
local MostDownloadedDataStore = DataStoreService:GetOrderedDataStore("MostDownloaded")

-- Test data, current solution
MostDownloadedDataStore:SetAsync("79d2bb8b-98b9-4178-8b01-3bdc769154a6", 10)
MostDownloadedDataStore:SetAsync("79d2bb8b-98b9-4178-8b01", 8)
MostDownloadedDataStore:SetAsync("79d2bb8b-98b9-4178", 16)

local MostDownloadedResult = MostDownloadedDataStore:GetSortedAsync(false, 25)

for _, ResultData in MostDownloadedResult:GetCurrentPage() do
	local CreationID = ResultData.key
	local CreationDownloads = ResultData.value
	
	-- Necessary to use a GetAsync call to get any data, bad
	local CreationData = CreationsDataStore:GetAsync(CreationID)
end

-- Better solution, store basic metadata about the creation in ordered data store
MostDownloadedDataStore:SetAsync(10, {ID = "79d2bb8b-98b9-4178-8b01-3bdc769154a6", Name = "Creation", UploaderID = "1"})
MostDownloadedDataStore:SetAsync(8, {ID = "79d2bb8b-98b9-4178-8b01", Name = "my creation", UploaderID = "123512875917189"})
MostDownloadedDataStore:SetAsync(16, {ID = "79d2bb8b-98b9-4178", Name = "wowza", UploaderID = "2883117"})

local MostDownloadedResult = MostDownloadedDataStore:GetSortedAsync(false, 25)

for _, ResultData in MostDownloadedResult:GetCurrentPage() do
	local CreationDownloads = ResultData.key
	local CreationMetadata = ResultData.value
	
	-- Creation metadata can be used to store basic info such as name and uploader ID
	-- The metadata can be used to show a high number of items in a browser, like the Roblox games page or catalog
	
	-- Only download full data when needed, such as when the creation is loaded into the game
	local CreationData = CreationsDataStore:GetAsync(CreationMetadata.ID)
end
3 Likes