Is there a way to retrieve random data from a datastore?s

My system saves a random unique code to a server datastore. All I want to do is pick a random key from my datastore and take the string containing information needed.

am I just the only who can see this?

You can use an OrderedDataStore to get every key

However, this is quite expensive, and you’ll reach the limit quite fast

What I would do is set the keys to an incrementing value. You would then be able to get how many keys you have saved and get a random number from 1 to that value to get a random value

Idk if this would work for you

local dss = game:GetService("DataStoreService")
local datastore:DataStore = dss:GetDataStore("TestServerStore"); -- Change name to the datastore

local function getKeys(datastore)
	local keys = {}
	local keyPages = datastore:ListKeysAsync();
	if keyPages then
		repeat
			local keyP = keyPages:GetCurrentPage()
			for _,k in keyP do
				table.insert(keys,k.KeyName)
			end
			keyPages:AdvanceToNextPageAsync()
			task.wait()
		until
		keyPages.IsFinished
		print(keys)
	end
	return keys
end

local dataKeys = getKeys(datastore); -- Returns a table of all the keys
--[[
Now you can use a very basic random number to get an index in the table then use the key associated with the random index to get the value associated with the datastore key.
]]--
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.