Why is the script still sending the request to the webhook when there is no value in the reason section?


  1. I’m trying to make a webhook system that players can use to report bugs and send game suggestions to my Discord Server.

  2. The issue is that the webhook request is still sent even when there’s no value in the reason value.

  3. I’ve tried the following: tostring(reason ~= nil) or tostring(reason ~= " "), I have no other ideas on what I can do to prevent this from happening.

  4. Proof: image


Script

local HttpService = game:GetService("HttpService")
local webhook = "https://discord.com/api/webhooks/822828495410298900/Ad8G9VuKpQ2wcIwpB0QQIwA2vFcTnr7gEAno13gRuQG13g7D4B-vr9gybEb2c8j4aIF5"
local groupid = 5006103

game.ReplicatedStorage.PlayerReport.OnServerEvent:Connect(function(plr,reason)
	if game.Players:FindFirstChild(plr.Name) then
		print(plr.Name)
		print(reason)
		if tostring(reason ~= "") or  tostring(reason ~= ".") or  tostring(reason ~= "hi") or tostring(reason ~= nil) or tostring(reason ~= " ") then
			local data = {
		["embeds"] = {{
			["title"] = "A report has been submitted!",
			["color"] = "3407616",
			["description"] = "Player Username:** " .. plr.Name.. "**." .. "\n Player ID:** " .. plr.UserId .. "**.\n\nUser Rank: **" .. plr:GetRoleInGroup(groupid) .. "**.\n\n**Report:** "..reason,
			["thumbnail"] = {url="http://www.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&Format=Png&userId="..plr.UserId},


		}
		}}
			local finaldata = HttpService:JSONEncode(data)
			HttpService:PostAsync(webhook, finaldata)	
		end
	end
end)

Thank you for any help you can give me.

Try:

if reason and not reason == nil then
  -- send request
end

Also if you’re checking for specific strings,

if tostring(reason ~= "hi") then

Change it to :

if tostring(reason) ~= "hi" then

Try

reason = reason:gsub("^%s*(.-)%s*$", "%1")
if reason ~= "" then
    -- request
end

Explanation:

"^%s*(.-)%s*$" is regex for “Get all text in between whitespace before and after the string”
^%s* means at the beginning ^ match any whitespace %s any number of times *
(.-) means match any characters (except new lines) . a minimal number of times (so that way we can match white space at the end of the text) - and put them into a group ()
%s*$ means match any whitespace %s at the end of a string $ any number of times *

Because we put our content in a capturing group, we can access it with %1 and anything inside that capturing group has no whitespace at the beginning or end of the text, so we’ve effectively trimmed the string.

EDIT:
Sorry, I didn’t mean to reply to you valtkins.

Alright so now, it doesn’t send on nil messages but it also doesn’t send on any messages…

@steven4547466, yours didn’t work at all. Thank you for trying though…

You want it to not send when the value is just "" right? reason is a string, too, right? If so, can you show me how you implemented it into your script, because this is what I get:

Try only the

if reason then
  --data sends
end

However if that works, then players will be able to send blank messages.

If this doesn’t work (if it doesn’t send messages at all), check if the reason is getting sent properly to the server through the RemoveEvent.

Nevermind, I made a massive error and didn’t have the webhook link updated… :sweat_smile:

Thank you for your help!

1 Like