Translating a player nick into the player object

Exactly how the title states it. I have the player’s nick, how do i get the player’s object (i need the steamid specifically) thanks.

[lua]
function GetPly(NickVariable)
for k, v in pairs(player.GetAll()) do
if v:Nick() == NickVariable then return v end
end
return false
end
[/lua]

something like this

Thank you.

oh well, it should look like this really :smiley:

[lua]
function GetPly(NickVariable)
for k, v in pairs(player.GetAll()) do
if v:Nick() == NickVariable then return v end
end
return false
end
[/lua]

and you should use it like this (you should add some checks if player exists etc.
[lua]
player = GetPly(SomeNick)
player:Kill()
[/lua]

or you can do this:
[lua]
function GetPlySteamID(NickVariable)
for k, v in pairs(player.GetAll()) do
if v:Nick() == NickVariable then return v.SteamID end
end
return
end

PlayerSteamID = GetPlySteamID(Nick)
[/lua]

I find this set of functions useful.

[lua]function player.FindByName(name)
name = name or “”
local playertbl = {}
for k , v in ipairs(player.GetAll()) do
if string.find(string.lower(v:Nick()) , string.lower(name)) then
table.insert(playertbl , v)
end
end
if #playertbl > 1 then
return false
else
return playertbl[1]
end
return false
end

function player.FindBySteamID(StID)
local playertbl = {}
for k , v in ipairs(player.GetAll()) do
if v:SteamID() == StID then
table.insert(playertbl , v)
end
end
if #playertbl > 1 then
return false
else
return playertbl[1]
end
return false
end

function player.FindByUniqueID(UnID)
local playertbl = {}
for k , v in ipairs(player.GetAll()) do
if v:UniqueID() == UnID then
table.insert(playertbl , v)
end
end
if #playertbl > 1 then
return false
else
return playertbl[1]
end
return false
end

function player.Find(info)
–return player.FindByName(info) or player.FindBySteamID(info) or player.FindByUniqueID(info) or false doesn’t work.
if ValidEntity(player.FindByName(info)) then
return player.FindByName(info)
elseif ValidEntity(player.FindBySteamID(info)) then
return player.FindBySteamID(info)
elseif ValidEntity(player.FindByUniqueID(info)) then
return player.FindByUniqueID(info)
else
return false
end
end[/lua]

I know about player:GetByUniqueID, but I don’t like the naming of it, so I re-wrote it. Also know the “more than one player” checks are unnecessary on all functions except for FindByName, but I kept it anyway.

Also, this only returns one player object - if more than one player is found it returns false. Fugly, yes. Bad code, yes. I was tired + felt like I had swine flu when I wrote it.

[lua] if #playertbl > 1 then
return false
else
return playertbl[1]
end
return false [/lua]

This is horrid.

[lua]
function player.FindByName(str)
for k,ply in ipairs(player.GetAll()) do
if ply:Name():lower():find((str or “”):lower()) then
return ply
end
end
return nil
end
[/lua]

Why making things difficult when they are easy? :eng101:

It is. But I need it only ever to return one target if more the one target is found, it returns false, not multiple targets. Also, wrote it while coughing my guts up + tired.

Its not always the greatest idea to get the meta from a nick, as people might have the same name. Not saying that it will ever happen, but its bordering on sloppy coding

Sometimes necessary, then you need to adopt even worse coding to make up for it (As shown by me)

What do you want me to do? Make a concommand that requires the user to input a steamid?

I didn’t say it was a bad idea. I just said that it could be flawed.

Kay
Speaking of concommands, how can i tell if one is being run by the rcon or not?

Rcon is not a player, so just do a check to see if it’s being run by an actual player.

Entity(0) is rcon.

[editline]11:48PM[/editline]

if ply == Entity(0) then

Body kits are accessories that are available in commercial market depending upon the model of car. These accessories aion kina make the car look daunting with added metallic part. These body kits give the cars with more astounding appearance leaving the admirers attracted. Also body kits are available for different car manufacturers like Ford, Chevy etc., Ford cars are customized with the addition of extra metallic layer using body kits. Body kit package of Ford cars can be of varied variety. For example body kits for Ford focus can range up to $150 USD. Also these prices can be added up with shipping charges when they are ordered from distant place.Online shopping of body kitsLord of the Rings Online GoldThe car owners can take less pain to acquire the body kits for the car. The car body kits can be acquired from the dealers present all over the world. The effectiveness of shopping can be brought up by the use of Internet. Online shopping has paved way for utilization of credit card payment system to institute any accessory purchase. Also LOTRO Gold this purchase can be secured by adopting credit card safe payment module. All that one needs to do is to properly world of warcraft gold identify body kits vendor and place a order. The order could be answered soon with a reply via e-mail. Also once the wow gold monetary transactions are made the body kits are shipped within 48 to 72 hours to respective destination. Online shopping has greatly reduced the work of shopper.


(User was permabanned for this post ("Spam." - DEADBEEF))

This is the one I use:

[lua]
function ASA.FindPlayerName( NAME )
local lNAME = string.lower(NAME)
for _, pl in pairs(player.GetAll()) do
local name = string.lower( pl:Nick() )
if string.find( name, lNAME ) then
return pl
end
end

return ASA.FindPlayerUserID(NAME)

end
[/lua]

[editline]05:58AM[/editline]

The ASA.FindPlayerUserID(NAME) goes to another function so I use one function to find anything property of the player.

@wowks Okay, before spamming with random bbcodes, make sure they actually work.

[lua]-- Get a player by a part of their name or UserID or UniqueID, with the option to use patterns.
function player.Get(name, usepatterns)
usepatterns = not usepatterns;
local num, res,len,lon = tonumber(name);
name = string.lower(name);
for _, ply in ipairs(player.GetAll()) do
if (num and (ply:UserID() == num or ply:UniqueID() == num)) then
return ply;
elseif (ply:Name():lower():find(name,1,usepatterns)) then
lon = ply:Name():len();
if (res) then
if (lon < len) then
res = ply;
len = lon;
end
else
res = ply;
len = lon;
end
end
end
return res;
end[/lua]
:colbert: