SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Thread Tools Display Modes
Old 28/06/2020, 04:51 AM   #1
Huge Clucker
PowerF's Avatar
Join Date: May 2014
Posts: 434
Reputation: 4
Default Find player by name

How do I find player by his name?
I know that you can use sscand but i can only make it work with command, i want to search player by name in a function
PowerF is offline   Reply With Quote
Old 28/06/2020, 07:24 AM   #2
Little Clucker
Join Date: Feb 2020
Posts: 35
Reputation: 22
Default Re: Find player by name

pawn Code:
stock GetName(playerid)
    new name[MAX_PLAYER_NAME];
    GetPlayerName(playerid, name, MAX_PLAYER_NAME);
    return name;

stock GetPlayerIdFromName(const playername[]) {
    foreach(new i : Player) {
        if(!strcmp(GetName(i), playername, true, strlen(playername)))
            return i;
algorhitmically is offline   Reply With Quote
Old 28/06/2020, 10:13 AM   #3
Huge Clucker
SkyFlare's Avatar
Join Date: Apr 2015
Location: NZ
Posts: 221
Reputation: 43
Default Re: Find player by name

Make sure if you use algorhitmically's code, which will work perfectly... make sure to remove the word "stock" from the start of the functions.
Purchasable Scripting Services Available, Inbox me with your Requests.
My Project Progress Tracker
My Work
S-Store System V2 LinkIngame Store in Dialogs
SkyFlare is online now   Reply With Quote
Old 28/06/2020, 10:43 AM   #4
Kwarde's Avatar
Join Date: Nov 2009
Location: The Netherlands
Posts: 2,740
Reputation: 1686
Default Re: Find player by name

To give some extra info about stock: Using keyword stock you indeed tell the compiler the piece of code is not required to use (thus not showing a warning if it's unused).
In gamemodes, you usually only create functions when you need them so the use of stock is really not needed. 'stock' is not a keyword you need to create a function. You can even declare variables as stock.

Apart from that:
pawn Code:
GetIDFromName(const name[])
    new ret_id;
    sscanf(name, "u", ret_id);
    return ret_id;
INVALID_PLAYER_ID is returned if the player wasn't found. -1 is returned if no name was entered. Otherwise it returns the playerid.
The upside of this is that it is faster than looping through players, comparing their name in strcmp(). And partly names work (if there aren't multiple matches).
If player "SuperPlayerxTF" is online, using GetIDFromName("Super") would return the ID of SuperPlayerxTF.
When the opportunity presents itself to flip-a da table, uh, you flip-a da table.

Discord: Kwarde#8009
Kwarde is offline   Reply With Quote

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Find player id (from name) GospodinX Scripting Help 2 13/01/2019 01:26 PM
How can i find if a player has a weapon ? bogdy_m12 Scripting Help 12 29/09/2012 12:13 AM
find player by name? +rep! PawnoQ Scripting Help 11 30/01/2012 02:18 PM
How to find the coords on a player ronnie3148 Help Archive 0 08/04/2011 02:31 PM
find player IP's when not connected [Ask]G_Sk8er Help Archive 2 23/01/2011 11:22 AM

All times are GMT. The time now is 09:26 AM.

Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.