• Seeing if an arg is a player
    16 replies, posted
Right, here's my current code [lua] function GetInfo(ply,cmd,args) target = (args[1]) for k, v in pairs(player.GetAll()) do if target -- Here's where I need help ply:ChatPrint("Steam Name: ".. target:SteamName()) ply:ChatPrint("RP Name: ".. target:Nick()) ply:ChatPrint("Steam ID: ".. target:SteamID()) end concommand.Add("user_info", GetInfo)[/lua] How would I check if target was a unique fragment of a Nick? It doesn't show, but the name of the concommand doesn't use a space.
[url]http://wiki.garrysmod.com/?title=Entity.IsPlayer[/url]
OK, maybe I need to be clearer. I need something like most admin addons have for like !kick, where I can type in a partial name and it finds the right guy.
Do you mean something like this? [lua] function GetInfo(ply,cmd,args) target = (args[1]) --This would need to be part of the players name, Kun(it) for example. for k, v in pairs(player.GetAll()) do if(string.find(v:Nick(),target) != nil) then --Searchs the players nickname for the "target" variable, if not nil it will print the info. v:ChatPrint("Steam Name: ".. v:SteamName()) v:ChatPrint("RP Name: ".. v:Nick()) v:ChatPrint("Steam ID: ".. v:SteamID()) end end end concommand.Add("user_info", GetInfo) [/lua]
Your concommand shouldn't have a space in it. Use a _ like Kunit did. [editline]18th December 2010[/editline] Mybad. The code thing cut off the underscore. [lua] for k,v in pairs(player.GetAll()) if not string.find(string.lower(v:Nick()), string.lower(args[1])) == nil then ply:ChatPrint("Steam Name: ".. v:SteamName()) ply:ChatPrint("RP Name: ".. vt:Nick()) ply:ChatPrint("Steam ID: ".. v:SteamID()) end end [/lua]
I would go test, but gmod dun broked.
[QUOTE=Amokov;26770200]I would go test, but gmod dun broked.[/QUOTE] It appears we all have this issue.
[lua] -- A function to get a player by a part of their name. function GetPlayerByArg(matchArg) for k, v in ipairs( player.GetAll() ) do if ( string.find(string.lower( v:Name() ), string.lower(matchArg), 1, true) ) then return v; end; end; return NULL; end; [/lua] [lua] local ply = GetPlayerByArg("joe"); if ( IsValid(ply) ) then -- Do stuff. end; [/lua]
Kunit, it worked, but it required the EXACT name of a player. Do you know how to make it accept partial names?
[QUOTE=Amokov;26799707]Kunit, it worked, but it required the EXACT name of a player. Do you know how to make it accept partial names?[/QUOTE] Use Kurozael's example, it should work fine.
[QUOTE=Amokov;26799707]Kunit, it worked, but it required the EXACT name of a player. Do you know how to make it accept partial names?[/QUOTE] ( string.find(string.lower( v:Name() ), string.lower(matchArg), 1, true) ) look at Kuro's example
[QUOTE=c-unit;26806232]( string.find(string.lower( v:Name() ), string.lower(matchArg), 1, true) ) look at Kuro's example[/QUOTE] Or just use Kuro's code.
[QUOTE=zzaacckk;26806732]Or just use Kuro's code.[/QUOTE] that's stolen from Kuras code..
[lua] function GetInfo(ply,cmd,args) target = (args[1]) for k, v in pairs(player.GetAll()) do if ( string.find(string.lower( v:Name() ), string.lower(target), 1, true) ) then --ply:ChatPrint("Steam Name: ".. target:SteamName()) ply:ChatPrint("RP Name: ".. target:Nick()) ply:ChatPrint("Steam ID: ".. target:SteamID()) end end end concommand.Add("user_info", GetInfo) [/lua] This is saying that Nick is a nil method? Triv, your cool for rating someone asking for help dumb.
It's probably because target is a string, and not an entity. besides, isn't it redundant to print out the targets name, when thats what the player has to type in? function GetInfo(ply,cmd,args) local players = {player.GetAll()} target = (args[1]) for k, v in pairs(players) do if ( string.find(string.lower( v:Name() ), string.lower(target), 1, true) ) then target = players[k] --ply:ChatPrint("Steam Name: ".. target:SteamName()) ply:ChatPrint("RP Name: ".. target:Nick()) ply:ChatPrint("Steam ID: ".. target:SteamID()) end end end concommand.Add("user_info", GetInfo) Try it, if it doesn't work, tough luck for both of us :3 Actually, kuros code above works perfectly, why not use that? [lua] -- A function to get a player by a part of their name. function GetPlayerByArg(matchArg) for k, v in ipairs( player.GetAll() ) do if ( string.find(string.lower( v:Name() ), string.lower(matchArg), 1, false) ) == 1 then return v; else ply:ChatPrint ("Either too many players were found, or that player does not exist.") return NULL; end; end; end; function GetInfo (ply,cmd,args) local target = GetPlayerByArg(args[1]); if ( IsValid(target) ) then --ply:ChatPrint("Steam Name: ".. target:SteamName()) ply:ChatPrint("RP Name: ".. target:Nick()) ply:ChatPrint("Steam ID: ".. target:SteamID()) end; concommand.Add("user_info", GetInfo) [/lua] Made a few edits, but should work just fine.
Well I assume he wanted to type in part of the name and get the entire name in the printout.
-snip- Figured it out.
Sorry, you need to Log In to post a reply to this thread.