Hey guys! This is my first time writing a webhook script from scratch, (I’m also semi-new to scripting, only about a month of experience) so go easy lol.
The idea of the script is to create an “on/off” logger that is automatic, in short, it’s supposed to send a webhook to the Discord server when a player switches to a specific team, and post another when they switch teams or leave the game.
I don’t get any errors, either, which is the weird thing.
I’m currently scripting the part when a player gets on a team and it sends a webhook, and I’ve used a bit of code for the time thing from this post.
The issue is, it’s not posting any webhooks to the discord at all, I’ve ran print tests (as you can see in the script attached below) and it doesn’t do anything. Any help would be appreciated. The script is located in StarterPlayerScripts and is a LocalScript.
local mandtwebhook = ""
local mdwebhook = ""
local ecwebhook = ""
local scdwebhook = "https://ptb.discordapp.com/api/webhooks/(thewebhookidishere)"
local mtfwebhook = ""
local aerowebhook = ""
local deawebhook = ""
local sdwebhook = ""
local plr = game.Players.LocalPlayer
local webhook = nil
local http = game:GetService("HttpService")
local function to12H(hour)
hour = hour % 24
return (hour - 1) % 12 + 1
end
local function getTime()
local date = os.date("*t")
return ("%02d:%02d"):format(((date.hour % 24) - 1) % 12 + 1, date.min)
end
local AMIndicator = script:WaitForChild("AMIndicator")
local TimeLabel = script:WaitForChild("PMIndicator")
while wait(1) do
local currentHour = os.date("*t")["hour"]
if currentHour < 12 or currentHour == 24 then
AMIndicator.Text = 'AM'
else
AMIndicator.Text = 'PM'
end
TimeLabel.Text = getTime()
end
plr:GetPropertyChangedSignal("Team")
print("works!")
if plr.team == "SCD" then
print("Webhookistheissue!")
webhook = scdwebhook
postwebhook()
print("webhookblocked?")
end
function postwebhook()
local date = os.date("!*t")
local Data = {
["content"] ="On @"..script.TextLabel.Text.."";
["username"] = ""..plr.Name.."";
}
Data = http:JSONEncode(Data)
http:PostAsync(webhook, Data)
end```
I don’t think HTTP POST Requests work on a local script, it would also be easy to retrieve the webhook link for exploiters if I am wrong. Also, is HTTP Requests enabled on the game settings?
HTTP Requests are enabled, I would put it in serverscriptservice but I don’t exactly know how to reference the LocalPlayer from a non-local script because I’m pretty sure you can’t use game.Players.LocalPlayer in a normal script.
Fire a remote event with your arguments. The player is already provided as the first argument in a function called by a remote event, so you won’t have to worry about the LocalPlayer argument. You can then use the webhook. That being said, this is not an ideal way to do this, nor a secure way, just a work-around for what you had requested.
Alright, I put that in the script, only issue is the print tests in the ServerSided script aren’t working.
Am I doing something wrong? The Print tests in the FireServer script work and I’m pretty sure it’s firing, but the Server Script isn’t getting it? Here’s the stuff that uses the FireServer.
local plr = game.Players.LocalPlayer
local fire = game.ReplicatedStorage:WaitForChild("LogStart")
function thing()
print("Works #1")
local team = plr.team
fire:FireServer(plr, team)
print("Fired!")
end
plr:GetPropertyChangedSignal("Team"):Connect(thing)
^ LocalScript under StarterPlayerScripts
local scdwebhook = "https://ptb.discordapp.com/api/webhooks/(webhookishere)/(webhookishere)"
local webhook = nil
local http = game:GetService("HttpService")
local log = game.ReplicatedStorage:WaitForChild("LogStart")
local function to12H(hour)
hour = hour % 24
return (hour - 1) % 12 + 1
end
local function getTime()
local date = os.date("*t")
return ("%02d:%02d"):format(((date.hour % 24) - 1) % 12 + 1, date.min)
end
local AMIndicator = script:WaitForChild("AMIndicator")
local TimeLabel = script:WaitForChild("PMIndicator")
while wait(1) do
local currentHour = os.date("*t")["hour"]
if currentHour < 12 or currentHour == 24 then
AMIndicator.Text = 'AM'
else
AMIndicator.Text = 'PM'
end
TimeLabel.Text = getTime()
end
function postwebhook()
local date = os.date("!*t")
local Data = {
["content"] ="On @"..script.TextLabel.Text.."";
["username"] = ""..plr.Name.."";
}
Data = http:JSONEncode(Data)
http:PostAsync(webhook, Data)
end
log.Event:connect(function(plr, team)
print("Works #2")
if plr.team == "SCD" then
print("Works #3")
webhook = scdwebhook
postwebhook()
print("Works #4")
end
end)
Oh my bad, you should do log.OnServerEvent:Connect(), you just forgot to account for how RemoteEvent’s are called and used .Event instead of .OnServerEvent. If there are more issues let me know.
I once again didn’t look at the code fully. I’m going to do that really quick after this post. When calling :FireServer(), the player argument is automatically provided. Additionally, you need to do player.Team.Name for the name of the team, since Team is an Object not a String.
I figured it out, the whole Time Function stuff was causing the script not to work and not post any errors, I mended it to work again and now the only issue I have is: 11:51:30.989 - ServerScriptService.Script:21: attempt to call a nil value
Not sure what’s nil though, here’s the current script:
local mandtwebhook = "1"
local mdwebhook = "1"
local ecwebhook = "1"
local scdwebhook = "https://ptb.discordapp.com/api/webhooks/nodontreradthis"
local mtfwebhook = "1"
local aerowebhook = "1"
local deawebhook = "1"
local sdwebhook = "1"
local webhook
local http = game:GetService("HttpService")
local fire = game.ReplicatedStorage:WaitForChild("LogStart")
local AMIndicator = script:WaitForChild("AMIndicator")
local TimeLabel = script:WaitForChild("PMIndicator")
fire.OnServerEvent:Connect(function(plr, Team)
print("Works #2")
player = plr
if player.Team.Name == "ScD" then
print("Works #3")
webhook = scdwebhook
postwebhook() -- Error
print("Works #4")
end
end)
local function to12H(hour)
hour = hour % 24
return (hour - 1) % 12 + 1
end
local function getTime()
local date = os.date("*t")
return ("%02d:%02d"):format(((date.hour % 24) - 1) % 12 + 1, date.min)
end
while wait(1) do
local currentHour = os.date("*t")["hour"]
if currentHour < 12 or currentHour == 24 then
AMIndicator.Text = 'AM'
else
AMIndicator.Text = 'PM'
end
TimeLabel.Text = getTime()
end
function postwebhook()
local Data = {
["content"] ="On @"..script.PMIndicator.Text.."";
["username"] = ""..player.Name.."";
}
Data = http:JSONEncode(Data)
http:PostAsync(webhook, Data)
end```