Inconsistent HTTP Requests

local HttpService = game:GetService("HttpService")
local Url = script.Parent.Url.Value
local ApiKey = script.Parent.ApiKey.Value

function SendErrorWebhook(errorMessage, info)
	local embeds = {{
		color = 0x0F37C6,
		description = "```\n" .. errorMessage .. "\n```",
		footer = {
			text = info
		}
	}}
	
	HttpService:PostAsync("SELF-HOSTED PROXY LINK",
		HttpService:JSONEncode({ ["embeds"] = embeds }),
		Enum.HttpContentType.ApplicationJson,
		false,
		{ webhook = "DISCORD WEBHOOK LINK" }
	)
end

function LogStateChange(Player, State)
	local Rank = Player:GetRoleInGroup(5009022)
	
	local success, result = pcall(function()
		return HttpService:PostAsync(
			Url .. "/api/player-state/" .. Player.UserId,
			HttpService:JSONEncode({
				state = State,
				username = Player.Name,
				rank = Rank,
			}),
			Enum.HttpContentType.ApplicationJson,
			false,
			{ Authorization = ApiKey }
		)
	end)
	
	if not success then
		SendErrorWebhook(result, "Player " .. (State == "Added" and "Joined" or "Left") .. " • " .. Player.Name .. " | " .. Rank)
	end
end

game.Players.PlayerAdded:Connect(function(Player)
	LogStateChange(Player, "Added")
end)

game.Players.PlayerRemoving:Connect(function(Player)
	LogStateChange(Player, "Removing")
end)

Above is my code, most of the time it sends the HTTP request to my server absolutely fine, but 10% of the time it fails to send a request. I added a logging function which sends the details to a Discord webhook via my custom proxy, but that hasn’t triggered (I have tested it many times and it works fine).

I would love to see the in-game console log when this happens, although as the issue is very inconsistent, I cannot debug it.

Any help would be appreciated.

1 Like