get the name of the closest player?
#1

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.
Reply
#2

Код:
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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)