Issue with Http Service and Discord Webhooks (Code 429)

I have an issue with Http Service and Discord Webhooks. I am trying to send a webhook every time when player hatch something rare, in studio it works exactly as it should, but in live server it gives me Code 429Too many requests. I did it exactly as it was recommended in other DevForum post because someone had simillar issue.

I tried many things but nothing seems to work (probably i am too stupid for this)
error_code2

There is a code of my WebhookService sending function:

function Module.SendWebhook(Name, ...)
	local url = Webhooks[Name]
	if not url then
		warn("[Webhooks] Invalid webhook name: " .. tostring(Name))
		return
	end
	local embed = Module.GetEmbed(Name, ...)
	if not embed then return end

	local payload = HttpService:JSONEncode({ embeds = { embed } })
	
	local requestData = {
		Url = url,
		Method = "POST",
		Headers = {
			["Content-Type"] = "application/json"
		},
		Body = payload
	}
	
	local Success, Result = pcall(function()
		return HttpService:RequestAsync(requestData)
	end)

	if not Success then
		warn(Result)
	else
		print("[WebhookService]: Successfully sent webhook to url: ".. url)
		print(`[Webhook: {Name}, Success? {Success}]:  StatusCode [{Result.StatusCode}], StatusMessage [{Result.StatusMessage}], Success [{Result.Success}]`)
	end
end

The issue is probably with how many HTTP requests that you are sending to each domains. Try to limit HTTP requests, and then there will not be problems.

You’re sending too many requests, you need to implement a rate limit/queue, or reduce how many you’re sending in the first place if that’s possible.

For example: you could batch multiple embeds together in one request (one message can have up to 10 embeds iirc).

function Module.SendWebhook(Name, ...)
	local url = Webhooks[Name]
	if not url then
		warn("[Webhooks] Invalid webhook name: " .. tostring(Name))
		return
	end
	local embed = Module.GetEmbed(Name, ...)
	if not embed then return end
	
	InsertToQueue(Name, embed, url)
end

local DEFAULT_INTERVAL = 1
local SEND_EVERY = 30
local LAST_SEND = 0

local RequestQueue = {}
function InsertToQueue(...)
	table.insert(RequestQueue, { table.unpack({...}) })
end

function RunQueue()
	while task.wait() do
		print(RequestQueue)
		if tick() - LAST_SEND >= SEND_EVERY and #RequestQueue then
			LAST_SEND = tick()
			local embeds = {}
			local url = ""
			local name = ""
			
			for Index, Table in ipairs(RequestQueue) do
				if Table[1] == "Hatched" and #embeds < 10  then
					name = Table[1]
					url = Table[3]
					local _i = table.find(RequestQueue, Table)
					table.remove(RequestQueue, _i)
					table.insert(embeds, Table[2])
				end
			end
			
			local payload = HttpService:JSONEncode(embeds)
			local requestData = {
				Url = url,
				Method = "POST",
				Headers = {
					["Content-Type"] = "application/json"
				},
				Body = payload
			}
			
			if url ~= "" and name ~= "" then
				for i = 1, 999 do
					LAST_SEND = tick()
					local Success, Result = pcall(function()
						return HttpService:RequestAsync(requestData)
					end)

					if not Success then
						warn(Result)
					else
						if Result.StatusCode ~= 429 and Result.Success then
							print("[WebhookService]: Successfully sent webhook to url: ".. url)
							print(`[Webhook: {name}, Success? {Success}]:  StatusCode [{Result.StatusCode}], StatusMessage [{Result.StatusMessage}], Success [{Result.Success}]`)
							break
						elseif Result.StatusCode == 429 and not Result.Success then
							warn(`[Webhook: {name}, Success? {Success}]: Attempt nr. {i}`)
						else
							print(`[Webhook: {name}, Success? {Success}]:  StatusCode [{Result.StatusCode}], StatusMessage [{Result.StatusMessage}], Success [{Result.Success}]`)
							break
						end
					end
					task.wait(DEFAULT_INTERVAL)
				end
			end
		end
	end
end

I’ve tried something like this, I know its bad but I don’t have enough knowledge with queues and http, It doesn’t work and it throws 400 (Bad Request)

I’m pretty sure Discord has blocked http requests from Roblox servers, which is probably why it still works in studio, but not in-game, so you’ll need a proxy.

You can try looking on the devforum for solutions, but I would recommend trying to make your own proxy:

This tutorial shows you how to make a Roblox proxy, but it shouldn’t be too hard to convert it into a Discord proxy.

Though, I wouldn’t recommend using Discord as a logging service, it’s against their TOS (I think). Theres a reason they blocked reqeusts from Roblox servers, and trying to bypass it is probably not a good idea.

I’m pretty sure they unblocked them, they seem to work in my games in live servers.

The weridest thing with it is that it sometimes sends it but sometimes not, I have my own proxy created (for likes counter feature) but how can I link it to the webhook

Thank you! this was the problem, I linked it with proxy and now it works

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