Datastore error: "Too many requests"

A day ago I released a new update for my game where people can create their own maps.
All the maps have a datastore key, but for some reason whenever I try loading all the keys from the datastore I get an error saying the datastore is getting too many requests. It will load maps but not all.

For some reason this only happens in Roblox itsself and not in studio.

``
local datastoreservice = game:GetService(“DataStoreService”)

local datastore = datastoreservice:GetDataStore(“BuildStore4”)
local folder = workspace.GameSettings.ListedMaps

function load()
local allkeys = datastore:ListKeysAsync()
–local allkeys = datastore:g
–leaderstats
while true do

	local current = allkeys:GetCurrentPage()

warn(“load”)

for f = 1, allkeys do
	for e,r in pairs(current) do


	warn(r.KeyName)

local current = v:GetCurrentPage()
local data

local success, errormessage = pcall(function()
	data = datastore:GetAsync(""..r.KeyName.."")
end)
if success then


	if data then
			for i,v in pairs(data) do
				warn(v)
			if workspace.GameSettings.ListedMaps:FindFirstChild(""..v[1].."") then
			else
				--local split = string.split(v[1],"|")
				local new = Instance.new("StringValue",workspace.GameSettings.ListedMaps)
				new.Name = v[1]
				new.Value = v[2]
				local likes = Instance.new("NumberValue",new)
				likes.Name = "Likes"
				likes.Value = v[3]
				local public = Instance.new("BoolValue",new)
				public.Name = "Public"
				public.Value = v[4]
				local mapname = Instance.new("StringValue",new)
				mapname.Name = "MapName"
				mapname.Value = v[5]

				local mapimage = Instance.new("NumberValue",new)
				mapimage.Name = "MapImage"
				mapimage.Value = v[6]
				
				local creator = Instance.new("StringValue",new)
				creator.Name = "Creator"
				creator.Value = v[7]
			end
		end
		
		
	end


else
	print("Data error")
	warn(errormessage)
	end

	end
	if allkeys.IsFinished then
		break
	end
	
	allkeys:AdvanceToNextPageAsync()
	
end
end

``

What am I doing wrong here?
Any help appreciated!

4 Likes

Can you show the output

also you can put codes like this:

code here

image

to show it like this

1 Like

Hello. Your ingame datastore budget is drastically lower than Studio’s budget. That’s why you’re getting such error, you’re basically spamming Datastore with requests.
Your code example is badly inserted, so please fix it for next readers.

However, if the code you inserted is chronologically correct, then it seems like you’re calling v:GetCurrentPage() too often even if you have the data needed, try caching it or loading it just once.
Other possibility might be, that you just have too many maps, which means you don’t have enough budget to load it all, find more here.

Also try reading this good post: Datastore important information

1 Like

I recommend you looking for some optimization methods on how to use Datastores.
Personally I’ve spent lot of time optimizing my datastore mechanism in my game, so instead of using ListKeysAsync() it’s better to have one key as a list of all maps.
More into detail: You could save a key allMaps which will contain a JSON array of all maps ["map0", "map1", "map2", ...], load the content of the key and then every second load a single map. Datastore budget is added up by one credit every second until maximum is reached. One get request costs you 1 credit, so instead of loading all the maps in one moment, which can consume all your budget very quickly, try spawning a task, which will periodically load a single map until the end is reached.

  1. Load data from key allMaps and save it into table named same
  2. Spawn a task using task.spawn
  3. Make a for loop in the task which will be used to load all the maps from allMaps key table
  4. On every end of one single loop add a task.wait(1) to give datastore a breathing space

This also means that map loading could take significant time until it’s done, but you can still tweak it a little and for example load first 30 maps at once and then spawn the task for loading the rest of the maps. Try experimenting.

2 Likes

Hmm, I already tried saving all maps in one key, but keys can only hold up to like 4 million characters. Still very much but won’t be enough to save all build data

2 Likes
15:55:06 DataStoreService: TooManyRequests: Too many requests, try again later. API:
DataStoreKeyPages: AdvanceToNextPageAsync, Data Store: BuildStore4
15:55:06
502: API Services rejected request with error. Error code: 0 Reason: Too many requests, try again later.
Stack Begin
Script 'ServerScriptService.Test', Line 84 function load
Script 'ServerScriptService. Test', Line 319
Stack End
1 Like

I think you should use another DataStore like ProfileService

1 Like

I’m not talking about saving all build data, but key names only. 4MB of space for all the keys should be enough.

I think you should of stored the builds data in the normal player’ datastore, and used references in ordereddatastores incase you needed to retrieve them based off of votes/likes/playtime etc.

OrderedDataStores cant save strings tho?

Why would you need to store a string?

Well for the mapName string right?

You can store that inside of the players data. the OrderedDataStore is only there so that you can reference the worlds with GetSortedAsync.

Ah, I think I know what you’re saying.
I’ll try something

1 Like

This will not solve your issue with sending too many requests if you have lot of maps.
Also OrderedDataStore GetSortedAsync request has even more strict budget. :slight_smile:

Hmm, datastores can have infinite keys right? Since millions of players have a key for their data

Yes, GetSortedAsync can give you max a 100 on page

The are three possible issues

1 - You are using too many DataStores for only one function, but this has a lower chance because I do not see you’re using way too many of them.

2 - Because you may be using one single DataStore to load huge amounts of data, if I were you, I would try to load the data from a Global Table instead.

3- You are using the function “ListAllKeys” which may result in failure if there are too many keys. Try using something else instead of ListAllKeys.

If this ever helps you find the solution, you’re welcome.

Writing an ordered-data-store wrapper is an option, to use caching.
Also, UI can be implemented to add sorting. This will certainly reduce the load.
I really don’t think that he will be going over datastore limits if he just stops loading all of the maps at once.

I totally agree on that. With some slight fixes and function changes, I believe he can overcome his issue.

One of the only advices I can give him is not using “ListAllKeys()”.