How to fix 502: API Services rejected request with error. HTTP 400 (Bad Request) error?

  1. What do you want to achieve? Keep it simple and clear!
    I was doing fine until i saw that one of my leaderboards were getting the error "502: API Services rejected request with error. HTTP 400 (Bad Request) " when someone has over 10^40 coins (or basically any currency)
  2. What is the issue? Include screenshots / videos if possible!
    Here is the script
local DTS = game:GetService("DataStoreService")
local Lb = DTS:GetOrderedDataStore("Lb1")
local DataStore = DTS:GetDataStore("DataStore3")
local function updateLeaderboard()
	local success, errorMessage = pcall(function()
		local Data = Lb:GetSortedAsync(false, 50,0,math.huge)
		local ClicksPage = Data:GetCurrentPage()
		for rank, data in ipairs(ClicksPage) do
			local user = game.Players:GetNameFromUserIdAsync(tonumber(data.key)) 
			local Name = user
			local PlayerUserId = "Player_"..data.key
			local LeaderboardData
			local success, errormessage = pcall(function()
				LeaderboardData = DataStore:GetAsync(PlayerUserId, data)
			end)
			local TimeWasted = math.floor(LeaderboardData.Currency)
			
			local isLeaderboard = false
			for i,v in pairs(game.Workspace.Leaderboard.SurfaceGui.ScrollingFrame:GetChildren()) do
				if v.Player.Text == Name then
					isLeaderboard = true
					break
				end
			end
			if TimeWasted and isLeaderboard == false then
				local newLeaderboard = game.ReplicatedStorage:WaitForChild("ScrollingFrame"):Clone()
				newLeaderboard.Player.Text = Name
				newLeaderboard.Points.Text = TimeWasted
				newLeaderboard.Ranks.Text = "#"..rank
				newLeaderboard.Position = UDim2.new(0,0, newLeaderboard.Position.Y.Scale + (.02 * #game.Workspace.Leaderboard.SurfaceGui.ScrollingFrame:GetChildren()), 0)
				newLeaderboard.Parent = game.Workspace.Leaderboard.SurfaceGui.ScrollingFrame
				if rank == 1 then
					local Description = game.Players:GetHumanoidDescriptionFromUserId(tonumber(data.key))
					local Dummy = workspace:WaitForChild("Dummy")
					Dummy.Humanoid:ApplyDescription(Description)
					newLeaderboard.Player.TextColor3 = Color3.fromRGB(239, 184, 56)
					newLeaderboard.Points.TextColor3 = Color3.fromRGB(239, 184, 56)
					newLeaderboard.Ranks.TextColor3 = Color3.fromRGB(239, 184, 56)
				end
			end
		end
	end)
	if not success then
		warn(errorMessage)
	end
end

while true do
	for _, player in pairs(game.Players:GetPlayers()) do 
		Lb:SetAsync(player.UserId, player.leaderstats.Currency.Value)
	end
	
	for _, frame in pairs(game.Workspace.Leaderboard.SurfaceGui.ScrollingFrame:GetChildren()) do
		frame:Destroy()
	end
	
	updateLeaderboard()
	
	wait(20)
end```
3. **What solutions have you tried so far?** Did you look for solutions on the Developer Hub?
I couldn't find one. This has been happening since last year

Home → Game Settings → Security → Enable API

Something like that. It’s like Enable HTTP requests or something like that.

Thats for the 502: API Services rejected request with error. HTTP 403 (Forbidden) error

I already have the api enabled

Let me put the game in my script, one second.

The script does work. But you need to have a lot of coins (like 10^50 coins)

Well for one, replace DataStoreService with DTS on Line 3. Lmao. Let me see tho.

Oh yeah sorry this is not my actual script

Oh alright, no problem haha. Er. So I mean datastores and Studio don’t really pair well. It does well in-game though. See if it works in an actual game rather than Roblox Studio.

Hello? Is anyone here? I need to fix this problem

Can you try print debugging to show where it’s erroring?

It errors when it refreshes for the second time

I think your value is just too high for the DataStore to handle. Here is something I found on the DevHub:


You can only store 4 million characters, so that could be your problem. The way to fix it would be to split the value across multiple keys.

If that was the case then it would’ve returned error code 105 instead of 502 though.

Im storing number values. Not strings

The limit of 4 million characters applies to regular DataStores, which are stored as serialized strings, unlike OrderedDataStores, which are being used in the case of this thread.

OrderedDataStore values are stored as unsigned 64-bit integer values, which have a maximum value of 2^63, which is 9.22 Quintillion (9.22 x 10^18).

There’s no way to store a value larger than this in an OrderedDataStore, but you can work around this limitation. For example, if you don’t need extreme precision, there are ways that you can shrink massive numbers down to be stored and then calculate the initial value when loading them. For example, you could store math.ceil(1000000 * math.log(value)) and then when displaying the values on the leaderboard, display math.exp(value_from_datastore / 1000000)

What this does is take the natural logarithm of the value (so for your example of 10^40, math.log(10^40) becomes 92.103403719762. When loading the value, you display math.exp(92.103403719762), which undoes the logarithm, giving you back 10^40. The thing is, because OrderedDataStores can only be integer values, you have to round the value, so you end up storing a value of 92, and math.exp(92) is 9.0176284050343 x 10^39, quite far from 10^40. The solution is that 1000000 - by multiplying by a large number, you’re able to store it with greater precision - you just need to remember to divide by the number when displaying the value.

Just as a note, math.log(0) is undefined, so you’ll actually get the same HTTP 400 error if you try and save that value. If you go with this method, make sure you skip the logarithm if the value is 0.

6 Likes

That is WAY to high for a datastore. You have to add a coin cap to prevent this. Or you could use the solution post by @DevSpec