Find player by name

I’m trying to add a console command for admins to my gamemode, which will work like this: When the admin types in console “weap [target name] [weapon name]”, the target player will get that weapon. But I have no idea how to make it find the player by their name. I know in Expression 2 in wiremod there was a function findPlayerByName(“name”), which would return the player entity even when the name parameter wasn’t the players full name, for example findPlayerByName(“jack”) could return the entity for any player whose name had “jack” in it. Is it possible to make anything similar in a gamemode using lua? I already have the code which gets parameters from the command, so basically I have a string, which the admin used in the command, for example “jack” and I want to find a player who has this in their name.

Arguments and a console command (;

Yes. I know I can use that. But the problem is that I need to first find the player on who I’m going to use ply:Give(“weapon”). So something like:
[lua]
function blah(ply, cmd, args)
local playername = args[1]
local weapon = args[2]
– find the player entity here, based on their name
local target = – HERE GOES THE CODE I DONT HAVE –
target:Give(weapon)
end
concommand.Add(“weap”, blah)
[/lua]
So if I type “weap jack weapon_crowbar” in the console, it will give me (JackYack13) or anyone who has “jack” in their name the crowbar.

[LUA]
local function PlayerFromName(name)
local n = string.lower(name)
local t = {}

for k,v in pairs(player.GetAll()) do
if(string.find(string.lower(v:Nick()), n)) then
table.insert(t,v)
end
end
return t[1] or “NIL”
end

function blah(ply, cmd, args)
local playername = args[1]
local weapon = args[2]
– find the player entity here, based on their name
local target = PlayerFromName(playername)
target:Give(weapon)
end
concommand.Add(“weap”, blah)

[/LUA]

Thanks! That’s probably what I’m looking for. I’m gonna test it to see if it works :smile: