I am trying to accomplish building a warning logger so whenever someone uses the command, it gets sent to a discord webhook. I know I have made something related to this topic a bit ago, but it’s not exactly the same. So… what I am trying to figure out is how to get the reasoning and all of the reasoning instead of just the first part, for some reason, when a I put a space in the reasoning, it just stops and gets the first part before the space. I know this sounds a little confusing but I couldn’t describe it, here’s the code, if that will help you.
Code:
local url = "webhook url"
local http = game:GetService("HttpService")
local warnings = 0
function getPlayer(shortcut)
local player = nil
local g = game.Players:GetPlayers()
for i = 1, #g do
if string.lower(string.sub(g[i].Name, 1, string.len(shortcut))) == string.lower(shortcut) then
player = g[i]
break
end
end
return player
end
game.Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(message)
local stringpeices = string.split(message, " ")
if stringpeices[1] == "!warn" then
local warnedplayer = getPlayer(stringpeices[2])
if warnedplayer.Name and game.Players:FindFirstChild(warnedplayer.Name) then
local reason = stringpeices[3]
local data = {
["username"] = player.Name.. " ("..player.UserId..")",
["content"] = player.Name.. " has reported ".. warnedplayer.Name.. " for: "..reason
}
local newdata = http:JSONEncode(data)
http:PostAsync(url,newdata)
end
end
end)
end)
Thank you for reading my topic and I hope someone can answer it and if you can’t thanks for reading it anyways!
This is not relevant to the string pattern issue, but I found another issue. It looks like you want to be able to get a player from a piece of their name, case-insensitive.
function getPlayer(shortcut)
local player = nil
local g = game.Players:GetPlayers()
for i = 1, #g do
if string.lower(string.sub(g[i].Name, 1, string.len(shortcut))) == string.lower(shortcut) then
player = g[i]
break
end
end
return player
end
This can fail. What if another player’s username starts with "na"? Will the command warn that other player, or will it warn you? I recommend just allowing case-insensitivity, but not allowing partial usernames.
By the way I found it messy so here is a simpler way
local function get_player_from_partial(partial)
partial = partial:lower()
for _, player in ipairs(Players:GetPlayers()) do
if player.Name:lower():sub(1, #partial) == partial then
return player
end
end
return nil
end