[NEED HELP] 999 Emergency Calling

Hi all,

I’m making a emergency call system, but I’m running into some hiccups. I’m using the HttpService to send a webhook to a Discord server channel, but I’m not sure what the problem is in the code.

This is a Script, not a LocalScript

local webhook = "https://discordapp.com/api/webhooks/WEBHOOKURL"
local httpservice = game:GetService("HttpService")
local description = script.Parent.Parent.Description.Text
local location = script.Parent.Parent.Location.Text

if location == nil then location = "No location provided."
if description == nil then description = "No description provided." end

script.Parent.MouseButton1Click:Connect(function()
local d = 
{
	["content"] = "",
	["embeds"] = {{
		["title"] = "999 Dispatch (Beta)",
		["description"] = "<@DISCORDSEREVRROLE>",
		["type"] = "rich",
        ["tts"] = false,
		["color"] = 4750552,
		["fields"] = {
			{
				["name"] = "Description",
				["value"] = description,
				["inline"] = false
			},
			{
				["name"] = "Location",
				["value"] = location,
				["inline"] = false
			},
            {
				["name"] = "Sent by",
				["value"] = game.Players.LocalPlayer.Name,
				["inline"] = false
			}
		}
	}}
}

   local fd = httpservice:JSONEncode(d)
httpservice:PostAsync(webhook, fd)
print("Webhook Sent!")
end)
end

This is a perfect moment to stress how important it is to format/indent your code. Here it is after reformatting:

local webhook = "https://discordapp.com/api/webhooks/WEBHOOKURL"
local httpservice = game:GetService("HttpService")
local description = script.Parent.Parent.Description.Text
local location = script.Parent.Parent.Location.Text

if location == nil then
    location = "No location provided."
    if description == nil then description = "No description provided." end

    script.Parent.MouseButton1Click:Connect(
        function()
            local d = {
                ["content"] = "",
                ["embeds"] = {
                    {
                        ["title"] = "999 Dispatch (Beta)",
                        ["description"] = "<@DISCORDSEREVRROLE>",
                        ["type"] = "rich",
                        ["tts"] = false,
                        ["color"] = 4750552,
                        ["fields"] = {
                            {
                                ["name"] = "Description",
                                ["value"] = description,
                                ["inline"] = false
                            },
                            {
                                ["name"] = "Location",
                                ["value"] = location,
                                ["inline"] = false
                            }, {
                                ["name"] = "Sent by",
                                ["value"] = game.Players.LocalPlayer.Name,
                                ["inline"] = false
                            }
                        }
                    }
                }
            }

            local fd = httpservice:JSONEncode(d)
            httpservice:PostAsync(webhook, fd)
            print("Webhook Sent!")
        end)
end

The first check actually wraps around the whole block, so your code only runs if location is nil. Both location and description will additionally never be nil, because they are text fields.

You might be looking to compare using == '' instead, and moving the last end in your script to the location right after the first if check.

2 Likes

Hey,

I wanted the code to run no matter what, the nils are for, if location is nil and description is nil send No description provided to the webhook, but if the description isn’t nil send the text vice versa.

That’s the issue I’m stuck with. :pensive: