Find a name with a partial string?

Okay so I want to have a chat command. I have been stuck on this for ages, So basicly you type !cmsg warning name message
Warning being the type, name being the player name and message being the message, I got the splitting part working fine. I was to make it so my name is KEMOSABY but for the name I only have to put kemo and it will find me like ULX does, i tried looking in there code but could not find it: What I got so far


		local players = player.GetAll()

			if(sendTo!="*") then

				print("Checking for playayers")

				for k,v in pairs(players) do
					print(string.match(sendTo,v:Nick()))
					if(string.match(sendTo,v:Nick(),-1)==string.lower(v:Nick())) then
		
						print("Compared "..sendTo.." Against"..v:Nick())

						ply:ChatPrint("Found player! "..v:Nick())

						sendMessage(ply , msgType , false , v)

						return nil

					end
				end

				ply:ChatPrint("ERROR : Could not find a player name which contains '"..sendTo.."'!")

				return nil

			end

This always come up with nil and the function sendMessage is never called becuase the statment is never true, Any help, Thanks

Evolve does lovely player name matching:

[Lua]function evolve:IsNameMatch( ply, str )
if ( str == “*” ) then
return true
elseif ( str == “@” and ply:IsAdmin() ) then
return true
elseif ( str == “!@” and !ply:IsAdmin() ) then
return true
elseif ( string.match( str, “STEAM_[0-5]:[0-9]:[0-9]+” ) ) then
return ply:SteamID() == str
elseif ( string.Left( str, 1 ) == “”" and string.Right( str, 1 ) == “”" ) then
return ( ply:Nick() == string.sub( str, 2, #str - 1 ) )
else
return ( string.lower( ply:Nick() ) == string.lower( str ) or string.find( string.lower( ply:Nick() ), string.lower( str ), nil, true ) )
end
end

function evolve:FindPlayer( name, def, nonum, noimmunity )
local matches = {}

if ( !name or #name == 0 ) then
	matches[1] = def
else
	if ( type( name ) != "table" ) then name = { name } end
	local name2 = table.Copy( name )
	if ( nonum ) then
		if ( #name2 > 1 and tonumber( name2[ #name2 ] ) ) then table.remove( name2, #name2 ) end
	end

	for _, ply in ipairs( player.GetAll() ) do
		for _, pm in ipairs( name2 ) do
			if ( evolve:IsNameMatch( ply, pm ) and !table.HasValue( matches, ply ) and ( noimmunity or !def or def:EV_BetterThanOrEqual( ply ) ) ) then table.insert( matches, ply ) end
		end
	end
end

return matches

end[/lua]

[editline]15th February 2015[/editline]

basic usage would be:

[lua]local matchedPlayers = evolve:FindPlayer( “matt” )

for k, ply in pairs(matched players) do
ply:ChatPrint(“hi matt”)
end[/lua]

Seems interesting, Thanks I will take a good look at this!

I have a shorter example which doesn’t have all of the functionality as the one posted by Matt but may be easier to understand ( depending on how well versed you are in logic, you may understand that one perfectly then this will be useless to you but hopefully it helps ):


//
// Find a player from their name
//
function FindPlayer( _name )
	_name = string.lower( _name );

	for k, v in pairs( player.GetAll( ) ) do
		if ( !IsValid( v ) ) then continue; end
		local _current = string.lower( v:Nick( ) );

		if ( string.find( _current, _name ) ) then
			return v;
		end
	end
end
	

I’d recommend adding another line to prevent it from returning you ( or an arg to skip a person )… This one also only returns the first match…