Webhook doesn't send, no errors or warnings

I have attempted to format my webhook to send information about a player every time they leave a game. It seems to not send, and shows no errors or warnings.

MY CODE:

local https = game:GetService("HttpService")
local plr = game:GetService("Players")
local hookURL = "https://discord.com/api/webhooks/-/-"
local runservice = game:GetService("RunService")

plr.PlayerAdded:Connect(function(p)
	runservice.Heartbeat:Connect(function()
		while wait(1) do
			
		end
	end)
end)

plr.PlayerRemoving:Connect(function(p)	
	local messageData = 
		{
			["contents"] = "",
			["username"] = "PlayerLogger",
			["avatar_url"] = "https://www.roblox.com/Thumbs/Avatar.ashx?x=500&y=500&Format=Png&userId="..p.UserId,
			["embeds"] = {{
				["title"]= "["..p.Name.."](https://www.roblox.com/users/"..p.UserId,
				["type"]= "rich",
				["color"]= tonumber(0x8034BA),
				["fields"]=
				{	
					{
						["name"]="Time In-Game",
						["value"]="This user has been in [this game](https://www.roblox.com/games/"..game.PlaceId..") for ".. p:WaitForChild("timeInGame").Value,
						["inline"]=true
					},
					{
						["name"]="Account Age",
						["value"]="This account has been existent for ".. p.AccountAge .." days.",
						["inline"]=true
					},
					{
						["name"]="Report User",
						["value"]="If you would like to report this user, please report them [here](https://www.roblox.com/abusereport/userprofile?id=".. p.UserId.. ")",
						["inline"]=false
					}

				},
				["footer"] = {
					["text"] = "Scripted by SpiralRBX."
				},
				["timestamp"] = os.date("!*t")
			}}}
	https:PostAsync(hookURL, https:JSONEncode(messageData))
	warn("Webhook Sent")
end)

EDIT: Typo fixes.

1 Like

Hello,

After reviewing the code, there are a few small little details to fix that would make this code up and running. First is to wrap your PostAsync in a pcall in case it fails and secondly is to adjust the table to fit into Discord’s standard table. An error here is being the “timestamp” section having os.date which is a table and therefore, your PostAsync fails. Here is the updated code below:

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

--// Variables
local WebhookUrl = "YOUR_WEBHOOK_URL_HERE"

--// Get Player Headshot
local function GetPlayerProfilePicture(UserId)
	return ("https://www.roblox.com/headshot-thumbnail/image?userId=".. tostring(UserId).."&width=420&height=420&format=png")
end

--// Send Webhook
local function SendWebhook(Player)
	local PlayerName = Player.Name
	local UserId = Player.UserId
	local AccountAge = Player.AccountAge
	local InTimeValue = Player:WaitForChild("timeInGame").Value
	local CurrentDate = DateTime.now()
	local CurrentDateString = CurrentDate:FormatUniversalTime("LL", "en-us")
	local Sent, Failed = pcall(function()
		local EncodedTable = HttpService:JSONEncode({
			["username"] = "PlayerLogger",
			["avatar_url"] = (GetPlayerProfilePicture(UserId));
			["embeds"] = {{
				["title"] = "[".. tostring(PlayerName).."](https://www.roblox.com/users/".. tostring(UserId)..")",
				["type"] = "rich",
				["color"] = 1059548,
				["fields"] = {	
					{
						["name"] = "Time In-Game",
						["value"] = "This user has been in [this game](https://www.roblox.com/games/"..game.PlaceId..") for ".. tostring(InTimeValue),
						["inline"] = true
					},
					{
						["name"] = "Account Age",
						["value"] = "This account has been existent for ".. tostring(AccountAge).." days.",
						["inline"] = true
					},
					{
						["name"] = "Report User",
						["value"] = "If you would like to report this user, please report them [here](https://www.roblox.com/abusereport/userprofile?id=".. tostring(UserId).. ")",
						["inline"] = false
					}
				},
				["footer"] = {
					["text"] = tostring(CurrentDateString)
				},
			}}
		})
		HttpService:PostAsync(tostring(WebhookUrl), EncodedTable)
	end)
	if not Sent and Failed then
		warn("Failed To Send Webhook! Error: ", tostring(Failed))
	end
end

Players.PlayerRemoving:Connect(function(Player)	
	SendWebhook(Player)
end)

Hopefully this helps!

1 Like