I am trying to make a warning logger but it does not get all of the reasoning at the end why is this?

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! :smile:
1 Like

Format your code properly in a code block.

```lua

```

I was trying to figure out how to, thanks for telling me.

Anyways now that you fix that, can you print out what you are getting and tell us what you want with some examples.

Okay, here is a picture of the command I am doing.


After my name, I’d like it to print all of the reasoning, yet, it only print the first segment, before the space.

I believe it isnt working because you are forgetting one step to the process. The embeds part. Try this.

local data = {
        ["embeds"] = {
            {
                ["username"] = player.Name.. " ("..player.UserId..")",
                ["description"] = player.Name.. " has reported ".. warnedplayer.Name.. " for: "..reason
                ["color"] = 16766976
            }
        }
    }
    local enData = httpService:JSONEncode(data)
    httpService:PostAsync(hookURL,enData)

The reason why it isn’t working is because string split causes all strings seperated by spaces to be placed in seperate indexes.

You would need to do:

local splitTable = string.split(your_string)
local str = ""
for i = 3, #splitTable do
   str = str .. splitTable[i]
end
-- then post the "str"

Yep that is supposed to happen, since string.split you are using it to split spaces, what you want to do is use string.match instead.

local command, target, argument = message:match("(!%a+) (%w+) (.*)")
if command == "warn" then
    -- get player with target
end

I explained that pattern in the last thread you made.


OT: @AbiZinho it would be pretty epic if split had an optional max_splits argument like python’s

It didn’t work, it only printed the first segment of my chat, after my name, sadly.

image

1 Like

Alright, I shall try this out, thanks.

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

Okay, thank you for that, although, now, it’s apparently not sending anything, when I do the command.

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message)
		local command, warnedplayer, reasoning = message:match("(!%a+) (%w+) (.*)")
		if command == "warn" and game.Players:FindFirstChild(warnedplayer) and reasoning then
				local data = {
        	["embeds"] = {
            {
                ["username"] = player.Name.. " ("..player.UserId..")",
                ["description"] = player.Name.. " has reported ".. warnedplayer " for: "..reasoning,
                ["color"] = 16766976
            }
        }
    }
				local newdata = http:JSONEncode(data)
				http:PostAsync(url,newdata)
		end		
	end)
end)

I am not sure what I messed up and sorry for keep going back and forth about this.

Print the results just before the if block.

print(command, warnedplayer, reasoning)

Also your indents are WAY off.

Alright I’ll try that and ya I know, I used the code that Magi provided me.

Thanks so much for helping me, it works now!