find player by name? +rep!
#1

hi,

how can i loop throught all players online and check if there is a player online with a certain name?
Hope you can help me
Reply
#2

Using for

pawn Код:
stock FindPlayerByName(name[])
{
    new Pname[24];
    for(new i; i<MAX_PLAYERS; i++)
    {
        if(!IsPlayerConnected(i)) continue;
        GetPlayerName(i, Pname, 24);  
        if(!strcmp(Pname, name, true)) return i;
    }
    return -1;
}
Foreach is similar, just replace the for loop.
Reply
#3

You can use the function I've made

pawn Код:
stock FindPlayerByName( name[] )
{
    for( new i = 0; i < MAX_PLAYERS; i += 1 )
    {
        if ( IsPlayerConnected( i ) )
        {
            new name2[24];
            GetPlayerName( i, name2, 24 );
            if ( strcmp ( name2, name, false ) == 0 )
            {
                return i;
                break;
            }
        }
    }
   
    return INVALID_PLAYER_ID;
}
Reply
#4

thx a lot
And how can i check it with this function then?
With an if statement somehow?
Reply
#5

pawn Код:
new name[MAX_PLAYER_NAME];
GetPlayerName(playerid, name, sizeof(name));
printf("player name %s was found by id: %d",name,FindPlayerByName(name));
Reply
#6

pawn Код:
if(FindPlayerByName("PawnoQ") != -1) return 1;
Return 1 if there is someone with the name PawnoQ online.
Reply
#7

thx a lot + rep
Reply
#8

sorry for the double post but how could i find out the playerid of a player with a certain name?
Reply
#9

oh, i should have answered here instead of the other topic, its a link to the sscanf plugin.
Reply
#10

would this code work to check if a player with a certain id and name is online?

pawn Код:
foreach(Player,i)
{
    if(FindPlayerByName(DOMPlayer)!= -1)//if player is online
    {
        new DOMname[24];
        GetPlayerName(i,DOMname,24);
        if(strfind(DOMname,"PawnoQ", true) == 0)//if player is the player with the right name
        {
            SetPlayerScore(i,GetPlayerScore(i)+20);//give only him score
        }
    }
    else
    {
        //if player is not online
    }
}



stock FindPlayerByName(name[])
{
    new Pname[24];
    foreach(Player,i)
    {
        GetPlayerName(i, Pname, 24);
        if(!strcmp(Pname, name, true)) return i;
    }
    return -1;
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)