Chat command w/ Nick() as argument, autocompletion
4 replies, posted
I'm attempting to make admin commands, and they're successful if I use Steam IDs or player IDs as arguments, but I'm trying to make it more user-friendly by making the argument for the commands a portion of a player's name. How would I be able to accomplish this? Currently I've only been able to do this with names by matching the argument to a player's name, but I want to be able to use just a PORTION of a player's name to execute a command on them (I.E. !slay dj would work instead of what I currently have to do, !slay |QIG| Djmuz).
Current code for the command I'm messing with. This is hooked to PlayerSay(ply, text)
[CODE]local splittext = string.Split(text," ")
if splittext[1] == "!slay" then
for k, v in pairs(player.GetAll()) do
ply:ChatPrint(string.sub(v:Nick(), 1, #splittext[2])) --DEBUG ONLY
if splittext[2] == string.sub(v:Nick(), 1, #splittext[2]) then
v:Kill()
else ply:ChatPrint("No targets found!") end end
return "" end [/CODE]
Snip read wrong
[QUOTE=djmuz;50686461]I'm attempting to make admin commands, and they're successful if I use Steam IDs or player IDs as arguments, but I'm trying to make it more user-friendly by making the argument for the commands a portion of a player's name. How would I be able to accomplish this? Currently I've only been able to do this with names by matching the argument to a player's name, but I want to be able to use just a PORTION of a player's name to execute a command on them (I.E. !slay dj would work instead of what I currently have to do, !slay |QIG| Djmuz).
Current code for the command I'm messing with. This is hooked to PlayerSay(ply, text)
[CODE]local splittext = string.Split(text," ")
if splittext[1] == "!slay" then
for k, v in pairs(player.GetAll()) do
ply:ChatPrint(string.sub(v:Nick(), 1, #splittext[2])) --DEBUG ONLY
if splittext[2] == string.sub(v:Nick(), 1, #splittext[2]) then
v:Kill()
else ply:ChatPrint("No targets found!") end end
return "" end [/CODE][/QUOTE]
The easy way is to do a string.find and see if they match, you should really consider what should happen if you have more than one match though. Here, you run it on all players with any part of their names matching.
[CODE]
for k,v in pairs(player.GetAll()) do
if string.find(v:Nick(),splittext[2],1,true) then
v:Kill()
end
end
[/CODE]
You could use [url=http://wiki.garrysmod.com/page/string/find]string.find[/url] or, if you want, [url=http://wiki.garrysmod.com/page/Patterns]learn about patterns[/url].
[QUOTE=NeatNit;50686806]You could use [url=http://wiki.garrysmod.com/page/string/find]string.find[/url] or, if you want, [url=http://wiki.garrysmod.com/page/Patterns]learn about patterns[/url].[/QUOTE]
Thanks, patterns fixed my issue.
Code is now [CODE]local pattern = "([%a]+)"
local splittext = string.Split(text," ")
if splittext[1] == "!slay" then
for k, v in pairs(splittext) do print(k, v) end
for k,v in pairs(player.GetAll()) do
if string.find(string.lower(v:Nick()),splittext[2],1,pattern) then
v:Kill()
QuantumNotify1(1, ply:Nick(), "slayed", v:Nick())
end end
return "" [/CODE]
Sorry, you need to Log In to post a reply to this thread.