How to Avoid HTTP 429 (Too Many Requests)?

Not always, seems to be a 50/50 chance, I’ll get an HTTP Error despite only running this code once:

local HttpService = game:GetService("HttpService")

local serverInfo = HttpService:JSONDecode(HttpService:GetAsync("http://ip-api.com/json/"))
local region = serverInfo.regionName
local country = serverInfo.country

Is there something I could change to avoid HTTP 429 (Too Many Requests)? Not sure why this errors shows up as the code isn’t running in a loop:

Here is the full script if needed:

-- Services
local HttpService = game:GetService("HttpService")
local MessagingService = game:GetService("MessagingService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

-- Variables
local Servers = ReplicatedStorage.Servers

-- ID
local JobId = string.split(game.JobId, '-')[1]
local cleanedJobId = string.gsub(JobId , '{', '')

-- Server Region
local serverInfo = HttpService:JSONDecode(HttpService:GetAsync("http://ip-api.com/json/"))
local region = serverInfo.regionName
local country = serverInfo.country

MessagingService:SubscribeAsync("ServerList", function(data)
	data = data.Data

	if data.serverId ~= cleanedJobId then
		local Name = #Servers:GetAttributes() + 1
		
		Servers:SetAttribute(`{Name}`, Name)
		Servers.ServersInfo:SetAttribute(`{Name}ID`, `{data.serverId}`)
		Servers.ServersInfo:SetAttribute(`{Name}Players`, `{data.players}`)
		Servers.ServersInfo:SetAttribute(`{Name}Region`, `{data.serverLocation}`)
		Servers.ServersInfo:SetAttribute(`{Name}Place`, `{data.serverPlace}`)

		task.wait(5)
		
		Servers:SetAttribute(`{Name}`, nil)
		Servers.ServersInfo:SetAttribute(`{Name}ID`, nil)
		Servers.ServersInfo:SetAttribute(`{Name}Players`, nil)
		Servers.ServersInfo:SetAttribute(`{Name}Region`, nil)
		Servers.ServersInfo:SetAttribute(`{Name}Place`, nil)
	end
end)

while game.PrivateServerId == "" do
	
	local data = {
		serverId = cleanedJobId,
		players = #Players:GetPlayers(),
		serverLocation = `{region}, {country}`,
		serverPlace = game.PlaceId
	}

	MessagingService:PublishAsync("ServerList", data)
	
	task.wait(5)
end

Retry every 10~30 seconds or so. It really depends on what service you use, since they each have their own ratelimit (or don’t have one at all)

Thanks, added this line using your advice and it fixed the issue:

-- Server Region
local serverInfo = HttpService:JSONDecode(HttpService:GetAsync("http://ip-api.com/json/"))

while not serverInfo do
	serverInfo = HttpService:JSONDecode(HttpService:GetAsync("http://ip-api.com/json/"))
	if serverInfo then
		break
	end
	task.wait(1)
end
1 Like

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