Chat commands with arguements

Hey guys, I know how to make chat commands that are simple, like chat commands that open menus, ect.

How can I have a chat command have arguements like console commands do? I’m trying to use the playersay hook but can’t figure out how.

EDIT: Sorry, misread your post.

GM:PlayerSay Override the say function and blocks everything else.

Do something like:
[lua]
function MahCommands( ply, text)
if string.sub(text, 0, 1) == “!” then – Is it a command?
local Temp = string.Explode(text," “) – Boooom!
local Com = string.sub(Temp[1],2,99) – Get the command
local Arg = “”
if #Temp>1 then – Will error if no arguments
local Arg = table.concat(Temp,” ",2,#Temp) – Get the argument
end

	if Com == "heal" then
		ply:SetHealth(ply:Health()+tonumber(Arg))
	end
	
end

end
hook.Add( “PlayerSay”, “MahCommands”, MahCommands);
[/lua]

This is probably the most inefficient way to do this, but here’s my try:



function GM:PlayerSay(ply, msg)
    if string.sub(msg, 0, 1) !== "/" then return end --If it's not a command, then end the function
    
    command1={}
    command1.command="/kill" -- The command
    command1.length=5 -- Command's length including the /
    
    command2={}
    command1.command="/potato" -- The command
    command1.length=7 -- Command's length including the /
    
    if string.sub(msg, 0, command1.length) == command1.command then
        -- Example: Player inputs "/kill Oprah"
        local arg = string.Explode(" ", msg)
        local argument = arg[2] -- argument = Oprah
    elseif string.sub(msg, 0, command2.length) == command2.command then
        -- Example: Player inputs "/potato Oprah Winfrey"
        local arg = string.Explode(" ", msg)
        local argument1 = arg[2] -- argument1 = Oprah
        --Do something with argument1
        
        local argument2 = arg[3] -- argument2 = Winfrey
        --Do something with argument2
        
    end
end


You might also want to use string.StartWith for checking for commands.

gosh no

[lua]
ChatCommands = {}
ChatCommands[“suicide”] = {}
ChatCommands[“suicide”].Run = function(ply,args)
ply:Kill()
end

local function docommand(ply,command,args)
if ChatCommands[command] == nil then return end
local command_ = ChatCommands[command]
command_:Run(ply,args)
end

function GM:PlayerSay(ply,msg)
local msgt = string.Explode(" “,msg)
if msgt[1] != nil && msgt[1][1] != nil && msgt[1][1] == “/” then
local command = string.gsub(msgt[1],”/","")
table.remove(msgt,1)
local args = msgt
docommand(ply,command,args)
end
end
[/lua]

abbreviated version of what I use, though I use a buffer and load individual commands from files with a function to add buffered commands to the table

the example command kills the player upon saying /suicide

arguments are args[1], args[2], etc

Here, Taken from a givecurrency command script i crapped out a bit back, hopefully it will help.
[lua]
hook.Add( “PlayerSay”, “pointgive”, function( ply, text, public )
if (string.sub(text, 1, 13) == “!givecurrency”) then
//Command start, Target, Token type, Amt – !givecurrency Archemyde ttt 100
local exp = string.Explode( " “, text)
if exp[1] == “!givecurrency” then – command
local target = exp[2]
local currency = exp[3]
local amount = exp[4]
print(target)
print(currency)
print(type(amount))
print(amount)
net.Start(“givepoints”)–send info to client, here to get by the concommand thingie
net.WriteString(exp[2])
net.WriteString(exp[3])
net.WriteString(exp[4])
net.Send(ply)
end
return false – dont put it into chat
end
end)
concommand.Add(“ps_sendcurrency”, function(sender, command, arguments)
if arguments[1] == nil or arguments[2] == nil or arguments[3] == nil then sender:ChatPrint(”!givecurrency target currency amount") return end
if tonumber(arguments[3]) < 0 then sender:ChatPrint(“You can’t send negative amounts to people!”) return end
–arguments = table.concat(arguments)
local matchcount = 0
matchcount = 0
local target
sender:ChatPrint(arguments[1])
for k, v in pairs (player.GetAll()) do-----------------Only one target?
if string.find(string.lower(v:Nick()), string.lower(arguments[1]),1) then
–sender:ChatPrint(string.find(string.lower(v:Nick()), string.lower(tostring(arguments[1]))))
matchcount = matchcount + 1
target = v
end
end
if matchcount == 1 then—Wee one target!
local currency --Wee currencies!
if string.lower(tostring(arguments[2])) == “ttt” then
currency = CURRENCY_TTT_TOKENS
elseif string.lower(tostring(arguments[2])) == “sdr” then
currency = CURRENCY_SURF_DR_TOKENS
elseif string.lower(tostring(arguments[2])) == “dosh” then
currency = CURRENCY_DOSH
else – Bad currency
sender:ChatPrint("That’s not a valid Currency! Valid currencies are: TTT , SDR, Dosh ")
return
end
if isnumber(math.ceil(tonumber(arguments[3]))) then-----------Got enough cash to give? is it even a valid num?
if sender:GetPSPoints(currency) >= tonumber(arguments[3]) then
--------Actually giving points!
target:AddPSPoints(math.ceil(tonumber(arguments[3])), currency, false)
target:ChatPrint(“You’ve received “…math.ceil(tonumber(arguments[3]))…” “…currency…” from “…sender:Nick()…”!”)
sender:SetPSPoints(sender:GetPSPoints(currency) - math.ceil(tonumber(arguments[3])), currency, false)
sender:ChatPrint(“You’ve sent “…math.ceil(tonumber(arguments[3]))…” “…currency…” to “…target:Nick()…”!”)

			elseif sender:GetPSPoints(currency) &lt; math.ceil(tonumber(arguments[3])) then -- Ur poor myte
			sender:ChatPrint("You don't have enough of that currency to give to that player!")
			end
		else--learn your times tables nub
		sender:ChatPrint("That's not a number! Command is: !givecurrency Targetname Currency Amount")
		end
	
	
	elseif matchcount &gt;= 2 then -- Too many similar names
		sender:ChatPrint(matchcount)
		sender:ChatPrint("Too many players with a similar name! Please be more specific.")
	elseif matchcount == 0 then
		sender:ChatPrint("No players Found!")
	end

end)

-----------------ClientSide---------------
net.Receive(“givepoints”, function(length)
local target = net.ReadString()
local currency = net.ReadString()
local amount = net.ReadString()
RunConsoleCommand(“ps_sendcurrency”,target,currency,tonumber(amount))
end)

[/lua]

I don’t know if you want this, but it’s a simple code that runs a kill command once you type something, you could probably change a few things, sorry if it’s not what you ment.