Hi, i was wondering how you add chat arguments? For example,lets say i wanted to do !phase "name". How would i go about doing this ?
Code Example:**DOES NOT WORK**
hook.Add("PlayerSay","phaser",function(ply,text)
if(text =="!phase ", "name")then
name:Kick("Example")
end
end)
You could use this, and detect with a space:
[url]https://wiki.garrysmod.com/page/string/Split[/url]
Then loop through everyone and see if their steam name matches the second table entry
[code]
hook.Add( "PlayerSay", "ChatCommandSlay", function( ply, msg, group )
if ( string.sub( msg, 1, 5 ) == "/slay" ) then
if ply:IsAdmin()
local plyk = string.sub( msg, 7 )
plyk:Kill()
else
ply:ChatPrint( "No Access" )
end
return false
end
end )
-- Untested, but should create the standard "slay" command ( /slay ply would kill ply)
[/code]
EDIT:
Or, if you want an example of what Niandra said:
[code]
hook.Add( "PlayerSay", "ChatCommandSlay", function( ply, msg, group )
local comchattable = string.Explode( " ", msg )
if ( comchattable[1] == "/slay" ) then
if ply:IsAdmin() then
for k,v in pairs( player.GetAll() ) do
if v = comchattable[2] then
v:Kill()
end
end
else
ply:ChatPrint( "No Access" )
end
return false
end
end )
-- Once again, untested but should work
[/code]
[QUOTE=warlock123;49513862][code]
hook.Add( "PlayerSay", "ChatCommandSlay", function( ply, msg, group )
if ( string.sub( msg, 1, 5 ) == "/slay" ) then
if ply:IsAdmin()
local plyk = string.sub( msg, 7 )
plyk:Kill()
else
ply:ChatPrint( "No Access" )
end
return false
end
end )
-- Untested, but should create the standard "slay" command ( /slay ply would kill ply)
[/code][/QUOTE]
string.sub would be very ineffective for this, since it gets messy if you use more arguments than 2. I'd use what Niandra said. The wiki page has a pretty good example as well
[QUOTE=warlock123;49513862][code]
hook.Add( "PlayerSay", "ChatCommandSlay", function( ply, msg, group )
if ( string.sub( msg, 1, 5 ) == "/slay" ) then
if ply:IsAdmin()
local plyk = string.sub( msg, 7 )
plyk:Kill()
else
ply:ChatPrint( "No Access" )
end
return false
end
end )
-- Untested, but should create the standard "slay" command ( /slay ply would kill ply)
[/code]
EDIT:
Or, if you want an example of what Niandra said:
[code]
hook.Add( "PlayerSay", "ChatCommandSlay", function( ply, msg, group )
local comchattable = string.Explode( " ", msg )
if ( comchattable[1] == "/slay" ) then
if ply:IsAdmin() then
for k,v in pairs( player.GetAll() ) do
if v = comchattable[2] then
v:Kill()
end
end
else
ply:ChatPrint( "No Access" )
end
return false
end
end )
-- Once again, untested but should work
[/code] [/QUOTE]
Hi, unfortunantely i was not able to get any of these to work.
I made slight edits to your code for debugging
[code]hook.Add( "PlayerSay", "AdminPhaseSystem", function( ply, msg, group )
local comchattable = string.Explode( " ", msg )
if ( comchattable[1] == "!sss" ) then
ply:ChatPrint("com1")
for k,v in pairs( player.GetAll() ) do
ply:ChatPrint(v:SteamID())
if (v == comchattable[2]) then
ply:ChatPrint(v:Nick())
v:Kill()
end
end
end
end ) [/code]
And it only gets up to posting SteamIDS [url]https://gyazo.com/e333e76eb425d8f19ffb318431df8796[/url].
As for the second one, i got this error [QUOTE] [ERROR] lua/autorun/argutester.lua:21: attempt to index a string value with bad key ('Kill' is not part of the string library)
1. error - [C]:-1
2. __index - lua/includes/extensions/string.lua:310
3. fn - lua/autorun/argutester.lua:21
4. unknown - addons/ulib/lua/ulib/shared/hook.lua:179 [/QUOTE]
EDIT: Got it working, heres my code for future peoples
[code]
hook.Add( "PlayerSay", "AdminPhaseSystem", function( ply, msg, group )
local comchattable = string.Explode( " ", msg )
if ( comchattable[1] == "!sss" ) then
ply:ChatPrint("com1")
for k,v in pairs( player.GetAll() ) do
ply:ChatPrint(v:SteamID())
if (comchattable[2] == v:Nick()) then
ply:ChatPrint(v:Nick())
v:Kill()
end
end
end
end )
[/code]
Now my question is, how can i get this working with two part names? Eg !sss Bill Bones -- or !sss "Bill Bones"
You could try to find partial names with [url]http://wiki.garrysmod.com/page/string/match[/url] and [url]http://wiki.garrysmod.com/page/string/find[/url]
Haven't done anything in Lua in months, but something like this?
[lua]
hook.Add( "PlayerSay", "", function( ply, msg, group )
local cmd = string.Explode( " ", msg );
if( cmd[1] == "!phase" ) then
for _, v in pairs( player.GetAll() ) do
if( cmd[2] && cmd[2]:lower():find( v:Name() ) ) then
v:Kick( "Example" );
end
end
end
end );
[/lua]
Is there any real advantage to using [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/string/Split]string.Split[/url] over [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/string/Explode]string.Explode[/url]?
[QUOTE=boxvader;49514893]Is there any real advantage to using [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/string/Split]string.Split[/url] over [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/string/Explode]string.Explode[/url]?[/QUOTE]
No. [URL="https://github.com/garrynewman/garrysmod/blob/3e138636eb1b0ad6ed785dedf350020755cff5f1/garrysmod/lua/includes/extensions/string.lua#L104-L106"]The only thing string.Split does is call string.Explode.[/URL] string.Explode's first argument is stupid, though. It should be the subject, not the separator, that comes first.
Sorry, you need to Log In to post a reply to this thread.