FindPlayer returning a nil value

hello, i am trying to make an admin mod but i failed at the second line :frowning: i am useing FindPlayer to find the selected player and stuff, look here:


function admin.slay(ply, cmd, args)
	playertoslay = FindPlayer(args[1])
	if ply:IsAdmin() then
		if playertoslay:IsPlayer() and !playertoslay:IsAdmin() then
			playertoslay:Kill()
		else
			print("sorry, no player is called " .. tostring(args[1]) .. " or he is an admin")
		end
	else
		print("sorry, you do not have access to that command")
	end
end

thats and example part of the code, and sadly all the admin mod is made like that and when i do “CG_Slay .:CG:. meisno” in console with “” arround mah name it gives me this in console :



autorun/server/adminmod.lua:53: attempt to call global 'FindPlayer' (a nil value)


any ideas? because if i can’t get the player then i cannot do anything and there does teh idea of an admin mod :frowning:

FindPlayer isn’t a valid function.

[lua]
function FindPlayer( name )
for _,v in ipairs( player.GetAll() ) do string.find( v:GetName(), name ) then return v end
return false
end[/lua]

Here’s a basic kind of thing for that.

Or if you want a better version, look below.

-snip- Entoros beat me to it.

[editline]01:44PM[/editline]

Well, almost. There are some syntax errors in his code.

[editline]01:45PM[/editline]

Ok, repost of code:

[lua]function player.FindByName(name)
local found = { };
for _, v in ipairs(player.GetAll()) do
if string.match(string.lower(v:Nick()), string.lower(name)) then
table.insert( found, v );
end
end
return found;
end[/lua]

Will return a table of players.

nope, it doesnt work :frowning:

What do you mean it doesn’t work? Are there any errors?

i am using entoros’s script and it gives me this error :frowning:



autorun/server/adminmod.lua:2: unexpected symbol near 'then'


any help?

Don’t use his, it has syntax errors.

aww… now i sound like a kiddy saying it like that, sorry, its just i need this to work or my server gets infested with minges and none of the admin mods work on mah server so i am making my own and i got all the functions planned out they work apart from the fact they can’t find the player

[editline]03:44PM[/editline]

so whose should i use because both of yours give me errors

[editline]03:46PM[/editline]

wait a sec, might…

[editline]03:46PM[/editline]

just testing it know loads gmod

=> sorry,its cause it didnt work befor

[editline]03:46PM[/editline]

nope, still doesnt work

[editline]03:50PM[/editline]

now its broken may mod :frowning: i get teh error :

btw, ignor the ligne number, its irelevant

autorun/server/adminmod.lua:88: attempt to call method ‘IsPlayer’ (a nil value)

[editline]03:50PM[/editline]

i am checking if the thing that is found is a player, should i remove thaty if i have your thing?

You can stop saying “mah”, and it will make you look less retarded.

ok, noted :smiley:

can someone help me with MY admin mod then?

The thing I posted returns a table of players, just for the sake of simplicity do something like this:
[lua]local players = player.FindByName(“whatever”)
local ply = players[1]
if ply:IsPlayer() then
– Code
end[/lua]

[lua]
function PlayerFind( ply, target )

players = {}

if not target then
ply:ChatPrint(“Please specify a player.”)
return;
end

for _, v in ipairs( player.GetAll()) do
if string.find( string.lower( v:Nick() ), string.lower( target ) ) != nil then
table.insert( players, v )
end
end

if #players >1 then
	ply:ChatPrint("Multiple targets found.")
	return nil

elseif #players <1 then
	ply:ChatPrint("No players found.")
	return nil

elseif #players == 1 then
	return players[1]
end

end
[/lua]

Here’s the search player function I’m using for my admin mod. It’s all commented. Enjoy.

[lua]// Serach through the players.
function SF.Utility.Search( Data )

// Fix data.
Data = SF.Utility.Fix( Data, false );

// Check data.
if ( !Data ) then
	
	// Return 1 - used for no players.
	return 1;
	
end

// Lower data.
Data = string.lower( Data );

// Count variable.
local Count = 0;

// Cycle through the players.
for _, pl in pairs( player.GetAll() ) do
	
	// If we find the data in their name.
	if ( string.find( string.lower( pl:Name() ), Data ) ) then
		
		// Add to the count variable.
		Count = Count + 1;
		
	end
	
	// If we find the data in their steam id.
	if ( string.find( string.lower( pl:SteamID() ), Data ) ) then
		
		// Add to the count variable.
		Count = Count + 1;
		
	end
	
	// If we find the data in their user id.
	if ( string.find( string.lower( pl:UserID() ), Data ) ) then
		
		// Add to the count variable.
		Count = Count + 1;
		
	end
	
end

// If there was only one match.
if ( Count == 1 ) then
	
	// Cycle through the players.
	for _, pl in pairs( player.GetAll() ) do
		
		// If we find the data in their name.
		if ( string.find( string.lower( pl:Name() ), Data ) ) then
			
			// Return the player.
			return pl;
			
		end
		
		// If we find the data in their steam id.
		if ( string.find( string.lower( pl:SteamID() ), Data ) ) then
			
			// Return the player.
			return pl;
			
		end
		
		// If we find the data in their user id.
		if ( string.find( string.lower( pl:UserID() ), Data ) ) then
			
			// Return the player.
			return pl;
			
		end
		
	end
	
// If there was more than one match found.
elseif ( Count > 1 ) then
	
	return 0;
	
// If no matches were found.
else
	
	return 1;
	
end

// Return false.
return false;

end[/lua]

You should check though… This is how I achieve and check it.
[lua] // Get the target of the command.
local Target = SF.Utility.Search( arg[ 1 ] );

// Check the target.
if ( Target == 0 ) then
	
	// Stop the function and notify the player.
	pl:ChatPrint( "More than one player was found with the specified arguments!" );
	return;
	
elseif ( Target == 1 ) then
	
	// Stop the function and notify the player.
	pl:ChatPrint( "No player with these arguments could be found." );
	return;
	
elseif ( !Target ) then
	
	// Stop the function and notify the player.
	pl:ChatPrint( "Couldn't execute the function." );
	return;
	
end[/lua]

You could probably code something that does that last bit, like a single function, but I don’t mind typing that bit out.

whats the target argument for, well infact can you explain everything for me because i dont understand your code combine, all i need is a simple function that will search for a player if i give part of a name, and return that entity

You are looping through the players more than you need to,

I just saw the poster above me’s post and realised that. I’ll change it hang on.

[editline]15:14[/editline]

There. Read the comments (//).

[lua]
// Serach through the players.
function SearchForPlayer( Data )

// Check data.
if ( !Data || Data == "" || Data == nil ) then
	
	// Return 1 - used for no players.
	return 1;
	
end

// Lower data.
Data = string.lower( Data );

// Count variable.
local Players = {  };

// Cycle through the players.
for _, pl in pairs( player.GetAll() ) do
	
	// If we find the data in their name.
	if ( string.find( string.lower( pl:Name() ), Data ) ) then
		
		// Add to the count variable.
		table.insert( pl, Players );
		
	end
	
	// If we find the data in their steam id.
	if ( string.find( string.lower( pl:SteamID() ), Data ) ) then
		
		// Add to the count variable.
		table.insert( pl, Players );
		
	end
	
	// If we find the data in their user id.
	if ( string.find( string.lower( pl:UserID() ), Data ) ) then
		
		// Add to the count variable.
		table.insert( pl, Players );
		
	end
	
end

// If there was only one match.
if ( #Players == 1 ) then
	
	// Return the player.
	return Players[ 1 ];
	
// If there was more than one match found.
elseif ( #Players > 1 ) then
	
	return 0;
	
// If no matches were found.
else
	
	return 1;
	
end

// Return false.
return false;

end

// Check a return from the search utility library.
function FixSearchPlayer( pl, Search )

// Make sure we're working with a player.
if ( !pl:IsValid() || !pl:IsPlayer() ) then
	
	// Exit.
	return;
	
end

// Check data.
if ( !Search || Search == "" || Search == nil ) then
	
	// Trying to search a nil string/entity.
	SF.Utility.Debug( "Tried to search a nill string/entity." );
	return false;
	
end

// If there was more than one player.
if ( Search == 0 )
	
	// Print & return.
	pl:ChatPrint( "More than one player found." );
	return false;
	
elseif ( Search == 1 ) then
	
	// No matches.
	pl:ChatPrint( "No players/matches were found." );
	return false;
	
end

// Exit.
return true;

end

// Example.
function TestingHai( pl, cmd, arg )

// Search for the player.
local Target = SearchForPlayer( arg[ 1 ] );
	Target = FixSearchPlayer( pl, Target );

// Check target.
if ( !Target ) then
	
	return;
	
end

// God.
Target:GodEnable();

end
concommand.Add( “TestingHai”, TestingHai );
[/lua]

gives me this:

autorun/server/adminmod.lua:83: attempt to call field ‘FindByName’ (a nil value)

Have you overridden the player library?

sorry elf, but your version seems over complicated and i have no idea on what function i would use to do what and its hard intergrating something that you dont understand into something that you do and you know what needs what

what i need is a function that will find a player say if i do

FindPlayer(hisname)

it would output his entity or whatever your people call it , the thing that i can ‘use’ in my script say for daplayer:Kick() or something

like that i could do

local daplayer = FindPlayer(auguments[1])
daplayer:Kick(tostring(arguments[2]))

[editline]04:21PM[/editline]

hmm… maker, good point reviewing code

[editline]04:22PM[/editline]

i am using ply as my first argument, but thats only because i am using the concommand.add function so i need it so i can do the check ply:IsAdmin()