SA-MP Forums Archive
get the name of the closest player? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: get the name of the closest player? (/showthread.php?tid=579630)



get the name of the closest player? - NoahF - 28.06.2015

Yes, I have searched the forums and ******d this, none of the answers helped.

I'm making a /wave command, and it should send a message saying "%s waves at %s", but i want the script to get the name of the closest player, instead of having to write an ID.


Re: get the name of the closest player? - Evocator - 28.06.2015

Код:
Float:GetDistanceBetweenPlayers(playerid, targetplayerid)
{
    new 
        Float:x1,
        Float:y1,
        Float:z1
    ;

    if (!IsPlayerConnected(playerid) || !IsPlayerConnected(targetplayerid))
    {
        return -1.00;
    }

    GetPlayerPos(playerid,x1,y1,z1);
    return GetPlayerDistanceFromPoint(targetplayerid, x1, y1, z1);
}


GetClosestPlayer(playerid)
{
    if (!IsPlayerConnected(playerid)) 
        return 0;

    new Float:cdist, targetid = -1;
    foreach (new i : Player)
    {
        if (GetPlayerState(i) == PLAYER_STATE_SPECTATING) continue;
        if (playerid == i) continue;

        if (targetid < 0 || cdist > GetDistanceBetweenPlayers(playerid, i))
        {
            targetid = i;
            cdist = GetDistanceBetweenPlayers(playerid, i);
        }
    }
    return targetid;
}

GetClosesName(playerid)
{
    new 
        Name[MAX_PLAYER_NAME + 1],
        id = GetClosestPlayer(playerid)
    ;

    GetPlayerName(id, Name, sizeof (Name));
    
    return Name;
}
This is an example, don't forget to check if the closest player is valid before getting the name.