I’m trying to make a webhook system that players can use to report bugs and send game suggestions to my Discord Server.
The issue is that the webhook request is still sent even when there’s no value in thereasonvalue.
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.
Proof:
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)
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.
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: