hello! i am right now working on a game that allows you to create little quotes and then view quotes made by other people
example
since the player can’t make multiple quotes, i am not having problems with tables, only with actually finding random keys that have the players quote tied to them
the game loads a list of 10 random quotes for you to see. i am currently using :listkeysasync and it is extremely show and it doesn’t really give you random keys.
i have searched some other posts and they all mention listkeysasync which does not work well at all because the keys load really slowly and :advancetonextpageasync() barely works half of the time
local t = {}
local list = ds:ListKeysAsync("",1)
local stop1 = false
local errorsize = 0
local s, e = pcall(function()
for i = 1,10 do
wait()
local l = list:GetCurrentPage()
for i, v in pairs(l) do
table.insert(t,(#t+1),v.KeyName)
end
if list.IsFinished then break end
errorsize = 0
repeat
local Success, Errormessage = pcall(function()
list:AdvanceToNextPageAsync()
end)
if not Success then
errorsize += 1
warn('Error Pages:AdvanceToNextPageAsync. Trying again')
warn(Errormessage)
wait(1)
end
until Success or errorsize >= 5
if errorsize >= 5 then
return true,t
end
end
end)
i am looking for a way to get completely random keys from the datastore that hold quotes very quickly. if there is a way for :listkeysasync() to load completely random keys at a good speed, then that is fine as well.
In the Roblox Datastore API, you can use the GetKeysAsync method to retrieve a list of all the keys in a datastore. You can then use the math.random function to select a random key from the list.
Here’s an example of how you might use these functions to retrieve a random key from a datastore in Roblox:
local DataStoreService = game:GetService("DataStoreService")
local MyDataStore = DataStoreService:GetDataStore("MyDataStore")
-- Retrieve a list of all the keys in the datastore
local keys = MyDataStore:GetKeysAsync()
-- Select a random key from the list
local randomIndex = math.random(1, #keys)
local randomKey = keys[randomIndex]
-- Use the random key to retrieve an entity from the datastore
local entity = MyDataStore:GetAsync(randomKey)
Keep in mind that retrieving a list of all the keys in a datastore can be a time-consuming operation, especially if the datastore contains a large number of keys. Depending on your use case, you may want to consider an alternative approach, such as maintaining a separate list of keys that you can use to randomly select keys from.
Using a separate datastore to store a list of keys can be a good way to avoid the performance overhead of calling GetKeys repeatedly. However, this approach has some potential drawbacks to consider:
You will need to update the list of keys whenever you add or remove keys from the original datastore. This can add some complexity to your code and may also increase the number of write operations you perform on the datastore.
You will need to retrieve the list of keys from the separate datastore each time you want to access a random key. This may add some additional latency to your code.
Overall, whether or not this approach is a good fit for your project will depend on your specific needs and constraints. If you have a small number of keys in your datastore and don’t mind the potential performance impact of calling GetKeys, then you may be able to use that method directly. On the other hand, if you have a large number of keys and need to access them frequently, then using a separate datastore to store the keys may be a better option.
The saving and loading of the table or data store may take longer if the list of keys is stored in a particularly big table or data store. Roblox data storage and tables can typically accommodate enormous amounts of data, but performance may suffer as the data size grows.
Even if the table containing the keys grows greatly in size, the table.insert and table.remove procedures shouldn’t be noticeably slower. The time it takes to insert or remove a member from the database is constant-time complex for these functions, so it doesn’t matter how big the table is. The expense of changing the table data structure, however, may still make adding or removing components from a very big database take slower.
It’s important to keep in mind that there are other elements that can impact how quickly your code executes, in addition to the size of the table or data store that contains the list of keys. The way you use the list of keys and how frequently you use the data storage will both affect how well your code performs. The efficiency of your code may also be affected if you regularly use the data store to retrieve or update values using the keys in the list.
Generally speaking, regardless of the volume of data you are working with, it is a good idea to design your code to be as efficient as possible. Using algorithms and data structures that are designed for the precise operations you need to carry out may be one way to achieve this, along with reducing the number of times you contact the data store or other external resources.
I hope that clarified things for you, fella!
If this has resolved your issue, please mark my response as “solved.”