Hello! I am trying to make a discord webhook that posts a message when someone touches a part. I want the message to include the username of the person at the start. But when I use the code it posts nothing and shows a error in the script. Here is my script:
local http = game:GetService('HttpService') local part = script.Parent if part.Touched then game.Players.PlayerAdded:connect(function(plr) local Data = { ["content"] = plr.Name.." has beaten "..part.Name } Data = http:JSONEncode(Data) http:PostAsync("discord webhook link here (i replaced this because I don't want anyone to use the link.))", Data) end
This script had the 'end' at the end have a red line underneath.
) Plus when I tried to, the webhook posted this: 'Players has beaten Mystery Stage' (this was a different script) Here is the second script I had problems with:
local http = game:GetService('HttpService') local player = game:GetService("Players") local part = script.Parent if part.Touched then local Data = { ["content"] = player.Name.." has beaten "..part.Name } Data = http:JSONEncode(Data) http:PostAsync("discord webhook link here (i replaced this because I don't want anyone to use the link.))", Data) end
Thank you.
All you have to do is add a closing bracket to the ‘end’ on line 11 (first script)
and for the second one you haven’t gotten the instance player so it outputs the variable name.
You just simply forgot to close off the function,
local http = game:GetService('HttpService') local part = script.Parent if part.Touched then game.Players.PlayerAdded:connect(function(plr) local Data = { ["content"] = plr.Name.." has beaten "..part.Name } Data = http:JSONEncode(Data) http:PostAsync("discord webhook link here (i replaced this because I don't want anyone to use the link.))", Data) end) end
Touched
is an event so it has to be like part.Touched:Connect(functionName)
You're just checking on who's touching the part, so the game.Players.PlayerAdded
is unnecessary. Touched
event has a parameter so we can detect if it's the player or not
The whole code looks like this now
local http = game:GetService('HttpService') local part = script.Parent part.Touched:Connect(function(hit) --Check if it's a player if hit.Parent:FindFirstChild("Humanoid") then --If it is a player, then store that player (workspace) into the plr variable local plr = hit.Parent local Data = { ["content"] = plr.Name .." has beaten ".. part.Name } Data = http:JSONEncode(Data) http:PostAsync("I've used discord webhook before, so I understand :P", Data) end end)