Finding Players via chat. (Without Ulib)

I am trying to figure out how to find a players entire name by just typing have of it and searching for results.
Let’s say I wanted to change a persons color to red when an admin types


!Red <PARTIAL SEGMENT OF PLAYERS NAME>"

I have this so far.
Would it work?
[lua]
function FindPly( ply, text, team, death )
if ( ply:IsAdmin() ) then
if ( string.Explode(1,text)=="!Red" ) then
local Foundply = ents.FindByName( string.Explode(2,text) )
Foundply:SetColor( 255, 0, 0, 255 )
end
end
end
hook.Add( “PlayerSay”, “FindPly”, FindPly )
[/lua]

Cooked this up for you:



--Returns a table of players that were matched
local function FindPlayer( partial_name )
	local found = {} --List of people we found matching that name
	for _,v in pairs( player.GetAll() ) do --Go through all the players
		local result = string.find( v:Name(), partial_name, 1, true ) --Look for the name
		if result then table.insert( found, result ) end --If it was there then add them to the list
	end
	return found --Return the results
end

function DoChatThingy( ply, text, team, death )
	if ( ply:IsAdmin() ) then --Admin only
		local sep = string.Explode( " ", string.lower( text ) ) --Make the text lowercase (so we can use !Red and !red) and then split it at every space, so we get "!red", "person1"
		if sep[1] == "!red" then --We've found the command we want
			local partial_name = table.concat( sep, " ", 2, #sep ) --Stitch the stuff together, so we don't just look for "[someclan]" in something like "[someclan] bob 2"
			local result = FindPlayer( partial_name ) --Look for people matching that name
			
			local count = #result --Find out how many people we found
			if count > 1 then --We found more than one, don't bother
				ply:ChatPrint( "More than one person found, change your argument" )
			elseif count == 0 then --We didn't find anybody
				ply:ChatPrint( "Didn't find anybody with that name" )
			else --We found 1 person, make them red
				result[1]:SetColor( 255, 0, 0, 255 ) --Make them red
			end
		end
	end
end
hook.Add( "PlayerSay", "Chatstuff", DoChatThingy )


Completely untested, so there might be a syntax error or something. But in theory it should work. :slight_smile:
Read the comments for a little insight into what that code does.

Thanks and the comments are really helpful, there is no point in copy and pasting if you don’t know what you are copying and pasting. :smiley:
Oh btw, If I was to add a third argument lets say the amount of redness,
Would I do…
[lua]
–Returns a table of players that were matched
local function FindPlayer( partial_name )
local found = {} --List of people we found matching that name
for _,v in pairs( player.GetAll() ) do --Go through all the players
local result = string.find( v:Name(), partial_name, 1, true ) --Look for the name
if result then table.insert( found, result ) end --If it was there then add them to the list
end
return found --Return the results
end

function DoChatThingy( ply, text, team, death )
if ( ply:IsAdmin() ) then --Admin only
local sep = string.Explode( " ", string.lower( text ) ) --Make the text lowercase (so we can use !Red and !red) and then split it at every space, so we get “!red”, “person1”
if ( sep[1] == “!red” and sep[3]<=0 and sep[3]>=255 ) then --We’ve found the command we want
local redness = sep[3]
local partial_name = table.concat( sep, " ", 2, #sep ) --Stitch the stuff together, so we don’t just look for “[someclan]” in something like “[someclan] bob 2”
local result = FindPlayer( partial_name ) --Look for people matching that name

		local count = #result --Find out how many people we found
		if count &gt; 1 then --We found more than one, don't bother
			ply:ChatPrint( "More than one person found, change your argument" )
		elseif count == 0 then --We didn't find anybody
			ply:ChatPrint( "Didn't find anybody with that name" )
		else --We found 1 person, make them red
			result[1]:SetColor( redness, 0, 0, 255 ) --Make them red
		end
	end
end

end
hook.Add( “PlayerSay”, “Chatstuff”, DoChatThingy )
[/lua]

Have you considered using rabbish’s chat command module? Gives you chatcommand.Add exactly the same as concommand.Add

I’d rather do it completely with lua.

The chatcommand module IS completely lua

I mean code it myself and in 2 lua files(client + server).

Yeah pretty much, although since string.Explode outputs a table of strings, you will want to use tonumber(). Otherwise you will get an error about trying to use :SetColor with a string.
e.g.:

[lua]
local redness = tonumber( sep[3] )
[/lua]