Hand-To Logs; Discord Webhook

Hey everyone! :wave:

I’ve just made a hand to system for my game and am wondering how I could implement some code in the following script to make it post a log to Discord using a webhook every time an item is handed to a player using the GUI. It would be something like “(PLAYER USERNAME) has handed (ITEM NAME) to (PLAYER USERNAME)!” The script is provided below, let me know if you can provide assistance with how to script this. Also, I know this isn’t the place to ask for scripts but I’ve looked everywhere and haven’t found anything of assistance. Thank you in advance for your assistance with this!

Local Script:

local Player = game.Players.LocalPlayer
local HandtoGui = script.Parent.Parent.Parent.Parent.FirstFrame
local NameBox = HandtoGui.SecondFrame.HandToGui.NameFrame.NameBox
local GiveButton = HandtoGui.SecondFrame.HandToGui.GiveFrame.GiveButton

local groupId = 7331168 --Group Id Here
local minimumRankToUseGui = 2 --Minimum Rank To Use Handto Gui Here

if Player:GetRankInGroup(groupId) < minimumRankToUseGui then
	HandtoGui:Destroy()
end

local function getPlayerFromPartialName(PartialName)
	local foundName = nil
	local Players = game.Players:GetPlayers()
	for i = 1, #Players do
		local PossiblePlayer = Players[i]
		if string.find(string.lower(PossiblePlayer.Name), string.lower(PartialName)) then
			foundName = PossiblePlayer.Name
		end
	end
	
	if not foundName then
		return nil
	else
		return foundName
	end
end
GiveButton.MouseButton1Click:Connect(function()
	if not Player.Character:FindFirstChildWhichIsA("Tool") then
		NameBox.Text = ""
		NameBox.PlaceholderText = "Equip a Tool First!"
		wait(1)
		NameBox.PlaceholderText = "USERNAME"
	end
	local NameBoxText = NameBox.Text
	if NameBoxText ~= "" then
		local playerName = getPlayerFromPartialName(NameBoxText)
		if playerName then
			print("Found player")
			game.ReplicatedStorage.GivePlayerItem:FireServer(playerName)
			NameBox.Text = ""
			NameBox.PlaceholderText = "Gave!"
			wait(1)
			NameBox.PlaceholderText = "USERNAME"
		else
			NameBox.Text = ""
			NameBox.PlaceholderText = "Player Not Found!"
			wait(1)
			NameBox.PlaceholderText = "USERNAME"
		end
	end
end)

Server Script:

game.ReplicatedStorage.GivePlayerItem.OnServerEvent:Connect(function(Player, PlayerName)
	if Player.Name ~= PlayerName then
	local ToolToGive = Player.Character:FindFirstChildWhichIsA("Tool")
		ToolToGive.Parent = game.Players[PlayerName].Backpack
		Player.leaderstats.Points.Value = Player.leaderstats.Points.Value + 1
		end
end)

There’s many varieties of ways to send a message, here’s the simplest

local HttpService = game:GetService("HttpService")

local Data = {
["content"] = "Someone has handed an item to someone!" -- Feel free to change this

}

Data = HttpService:JSONEncode(Data)
HttpService:PostAsync("WebHookURLHere", Data)

Just put this in the function, after the item is handed.

Ah, okay! Also, I forgot to include the format that I’d like in the post. Please see the bolded section of the topic. Also, how would I reference the player username that’s handing the item, the item name (tool name), and the player that received the item?

So this is in a local script right?

Just add extra params in the FireServer! (The player name, and item)
And then make the PostAsync on the server side.

local Data = {
["content"] = Player.Name.." has handed "..ItemParam.." to "..PlayerReceivingParam.."!"
}

Okay! But, would I do something like local ItemParam = something? If so, what would I put for the variables?

Hmm… Maybe when you reference the Tool with :FindFirstChildWhichIsA()?

local Tool = Player.Character:FindFirstChildWhichIsA("Tool")
Event:FireServer(Tool)

So sorry, I forgot to include the ServerScript in the post. Please see the topic for the attached serverscript and let me know how I would rewrite the serverscript code to include the discord webhook along with the appropriate references.

game.ReplicatedStorage.GivePlayerItem.OnServerEvent:Connect(function(Player, PlayerName)
	if Player.Name ~= PlayerName then
	local ToolToGive = Player.Character:FindFirstChildWhichIsA("Tool")
		ToolToGive.Parent = game.Players[PlayerName].Backpack
		Player.leaderstats.Points.Value = Player.leaderstats.Points.Value + 1
                local Data = {
                ["content"] = Player.Name.." has handed "..ToolToGive.Name.." to "..PlayerReceivingParam.."!"
}
Data = HttpService:JSONEncode(Data)
HttpService:PostAsync("WebHookURLHere", Data)
		end
end)
2 Likes

What would the “PlayerReceivingParam” be?

The player that is receiving the tool, which would be PlayerName

1 Like

I believe it would be PlayerName, as that is the player you are giving the tool to.

1 Like