Reporting Though PlayerSay String

I’m working on a script that is supposed to return “You have reported “Name” for “Reason”” to the person who is reporting, but for some reason, when I do this, it doesn’t work, nor does it ever return anything. Knowing me, it is probably all wrong, so I’m coming to get you guys’ help. Whenever I re-run the script with the lua_openscript command, I get no errors, but whenever I type /report (ANYTHING HERE), I get:


Hook 'ReportUser' Failed: [@lua\autorun\server\report.lua:2] bad argument #1 to 'sub' (string expected, got nil)

Can someone please tell me what’s wrong with it?
[lua]hook.Add(“PlayerSay”, “ReportUser”, function(ply, cmd, args)
if string.lower(string.sub(text, 1, 7 )) == “/report” then
local Players = {}
local Reporter = ply
if(!ValidEntity(Reporter)) then return end
local Reported = args[1]
local Reason = args[2]

if(!args[1]) then
    Reporter:ChatPrint("Please specify a player to report.")
end

if(!args[2]) then     
    Reporter:ChatPrint("Please specify a reason to report this player.")
    return
end

for _, v in pairs(player.GetAll()) do
    if string.find(string.lower(v:Nick()), string.lower(Reported)) then
    table.insert(Players,v)
end

if #Players <= 0 then
    Reporter:ChatPrint("Sorry, no players were found using that name.")
    return
end
if #Players > 1 then
    Reporter:ChatPrint("Sorry, multiple players matched what you entered.  Try making the name more specific.")
    return
end

Reported = Players[1]
Reporter:ChatPrint("You have reported " .. Reported:Nick() .. " for " .. Reason .. ".")

end
end
end)[/lua]

You never declared a “text” variable.

Your mistake is in that you thought of the PlayerSay as a concommand, whereas it is not. The arguments you passed to the function are player, command, and arguments, but the arguments passed to PlayerSay are player, text, and toall.

Basically, change the argument names.

Thanks. That fixed the error, but it’s still not working (It’s just doing nothing). This is what I have.

[lua]hook.Add(“PlayerSay”, “ReportUser”, function(player, text, toall)
if string.lower(string.sub(text, 1, 7 )) == “/report” then
local Players = {}
local Reporter = ply
if(!ValidEntity(Reporter)) then return end
local Reported = args[1]
local Reason = args[2]

if(!args[1]) then
    Reporter:ChatPrint("Please specify a player to report.")
end

if(!args[2]) then     
    Reporter:ChatPrint("Please specify a reason to report this player.")
    return
end

for _, v in pairs(player.GetAll()) do
    if string.find(string.lower(v:Nick()), string.lower(Reported)) then
    table.insert(Players,v)
end

if #Players <= 0 then
    Reporter:ChatPrint("Sorry, no players were found using that name.")
    return
end
if #Players > 1 then
    Reporter:ChatPrint("Sorry, multiple players matched what you entered.  Try making the name more specific.")
    return
end

Reported = Players[1]
Reporter:ChatPrint("You have reported " .. Reported:Nick() .. " for " .. Reason .. ".")

end
end
end)[/lua]

[lua]local Reporter = ply[/lua]

Don’t you mean player instead of ply?

I have
[lua]hook.Add(“PlayerSay”, “ReportUser”, function(player, text, toall)
if string.lower(string.sub(text, 1, 7 )) == “/report” then
local Players = {}
local Reporter = player
if(!ValidEntity(Reporter)) then return end
local Reported = text[1]
local Reason = text[2]

if(!text[1]) then
    Reporter:ChatPrint("Please specify a player to report.")
    return
end

if(!text[2]) then     
    Reporter:ChatPrint("Please specify a reason to report this player.")
    return
end

for _, v in pairs(player.GetAll()) do
    if string.find(string.lower(v:Nick()), string.lower(Reported)) then
    table.insert(Players,v)
    end
end

if #Players <= 0 then
    Reporter:ChatPrint("Sorry, no players were found using that name.")
    return
end
if #Players > 1 then
    Reporter:ChatPrint("Sorry, multiple players matched what you entered.  Try making the name more specific.")
    return
end

Reported = Players[1]
Reporter:ChatPrint("You have reported " .. Reported:Nick() .. " for " .. Reason .. ".")

end
end)[/lua]

But now whenever I type /report (Anything), I just get “Please specify a player to report.”

“text” is not a table afaik, it’s a string. If you want to put every word into a new field in a table, explode the text:
[lua]
hook.Add(“PlayerSay”, “ReportUser”, function(pl, text, toall)
local txt = string.Explode(" ", text)
PrintTable(txt)
if string.lower(txt[1]) == “/report” then
local Players = {}
local Reporter = pl
if(!ValidEntity(Reporter)) then return end
local Reported = txt[2]
local Reason = txt[3]

    for i=4,#txt do
        Reason = Reason .. " " .. txt*
    end 
     
    if(!Reported) then 
        Reporter:ChatPrint("Please specify a player to report.") 
        return 
    end 
      
    if(!Reason) then      
        Reporter:ChatPrint("Please specify a reason to report this player.") 
        return 
    end 
      
    for _, v in pairs(player.GetAll()) do 
        if string.find(string.lower(v:Nick()), string.lower(Reported)) then 
        table.insert(Players,v) 
        end 
    end 
      
    if #Players <= 0 then 
        Reporter:ChatPrint("Sorry, no players were found using that name.") 
        return 
    end 
    if #Players > 1 then 
        Reporter:ChatPrint("Sorry, multiple players matched what you entered.  Try making the name more specific.") 
        return 
    end 
      
    Reported = Players[1] 
    Reporter:ChatPrint("You have reported " .. Reported:Nick() .. " for " .. Reason .. ".") 
end 

end) [/lua]

tested & working

Okay, I have this:
[lua]hook.Add(“PlayerSay”, “ReportUser”, function(player, text, toall)
local txt = string.Explode(" ", text)
if string.lower(txt[1]) == “/report” then
local Players = {}
local Reporter = player
if(!ValidEntity(Reporter)) then return end
local Reported = txt[2]
local Reason = txt[3]

    if(!Reported) then  
        Reporter:ChatPrint("Please specify a player to report.")  
        return  
    end  
      
    if(!Reason) then       
        Reporter:ChatPrint("Please specify a reason to report this player.")  
        return  
    end  
      
    for _, v in pairs(player.GetAll()) do  
        if string.find(string.lower(v:Nick()), string.lower(Reported)) then  
        table.insert(Players,v)  
        end  
    end  
      
    if #Players <= 0 then  
        Reporter:ChatPrint("Sorry, no players were found using that name.")  
        return  
    end  
    if #Players > 1 then  
        Reporter:ChatPrint("Sorry, multiple players matched what you entered.  Try making the name more specific.")  
        return  
    end  
      
    Reported = Players[1]  
    Reporter:ChatPrint("You have reported " .. Reported:Nick() .. " for " .. Reason .. ".")  
end  

end)[/lua]
But I’m getting:



Hook 'ReportUser' Failed: [lua\autorun\server\report.lua:20] attempt to call field 'GetAll' (a nil value)

I updated my post above; you put player as an argument, which is a protected metatable name. I replaced it with pl and made some other changes. It works now.

-snip-

I now have:
[lua]hook.Add(“PlayerSay”, “ReportUser”, function(pl, text, toall)
local txt2 = text
local txt = string.Explode(" ", text)
if string.lower(txt[1]) == “/report” then
local Players = {}
Reporter = pl
if(!ValidEntity(Reporter)) then return end
local Reported = txt[2]
if(!Reported) then
if(!txt[3]) then
Reporter:ChatPrint(“Please specify a name and reason to report this player.”)
return
end
else
for _, v in pairs(player.GetAll()) do
if not string.find(string.lower(v:Nick()), string.lower(Reported)) then
Reporter:ChatPrint(“Sorry, no players were found using that name.”)
return
else
if(!txt[3]) then
Reporter:ChatPrint(“Please specify a reason to report this player.”)
return
else

                    string.find(string.lower(v:Nick()), string.lower(Reported))
                    table.insert(Players,v)
                    if #Players > 1 then
                        Reporter:ChatPrint("Sorry, multiple players matched what you entered.  Try making the name more specific.")
                    return
                    else
                        local Length = string.find(txt2, txt[3])
                        Reason = string.sub(txt2, Length)
                    end
                        Reporter:ChatPrint("You have reported " .. v:Nick(Reported) .. " for: " .. Reason .. ".")
                        for k, v in pairs(player.GetAll()) do
                            if v:IsAdmin() then
                                v:ChatPrint(v:Nick(Reporter) .. " has reported " .. v:Nick(Reported) .. " for: " .. Reason .. ".")
                            return
                            end
                        return
                        end
                    return
                end
                return
            end
            return
        end
    return
    end
    return
end

end)[/lua]
But if I correctly type a part of a name plus more info, it will go into the reason.

For example:
/report Mister Man Naab

Because there was only one person with the name “Mister Man”, it goes through, but it shows “You have reported Mister Man for: Man Naab.” I know what’s wrong and why it’s doing that, but I can’t really think up a fix for it. Any ideas?

I see what it’s doing!

/report “Mister” “Man Naab”

It’s finding the first argument in your say matching it with the person then adding man naab seperate try.

/report “Mister Man” “Naab Some reason”

Thanks for the help, idiot. Anybody else who actually read my whole post willing to help?

How rude. I’m curious if someone will help him now after he calls someone a idiot after the person trying to do somthing nice.

Use " " to separate arguments.

That won’t fix it though.