SA-MP Forums Archive
How to check if player is online? - 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: How to check if player is online? (/showthread.php?tid=317645)



How to check if player is online? - Dr.Heinz - 12.02.2012

i create a i love you system for my server. but the problem is everytime i say
i love you xenia, it says Sweet! "Server-Owner Heinz tell xenia i love you" i want to
know how to check if she is online. here is the code onplayertext

pawn Код:
if(!strcmp(text, "i love you xenia", true))
    {
       if(!strcmp(pname, "Heinz", true))
       {
          format(string, sizeof(string), "Server-Owner %s has tell xenia i love you", pname);
          SendClientMessageToAll(COLOR_GREEN, string);
          printf(string);
          GameTextForPlayer(playerid, "~g~Sweet!", 2000, 3);
       }
       else
       {
          format(string, sizeof(string), "%s(ID: %d) has been banned for telling xenia i love you", pname, playerid);
          SendClientMessageToAll(COLOR_RED, string);
          GameTextForPlayer(playerid, "~r~Shutup!", 2000, 3);
          BanEx(playerid, "Fuck-off");
       }
       return 0;
    }
-Heinz


Re: How to check if player is online? - nmader - 12.02.2012

Have you tried something like:
pawn Код:
If(IsPlayerIsConnected)
Or is that not what you meant?


Re: How to check if player is online? - Germanator - 12.02.2012

if(IsPlayerConnected(ReturnUser("xenia")))

That should work just fine.


Re: How to check if player is online? - Nuke547 - 12.02.2012

if(IsPlayerConnectedEx(id) == 1)


Re: How to check if player is online? - Dr.Heinz - 12.02.2012

i mean i want to check if xenia is online. and it is not possible to use IsPlayerConnected to check if she is online

Edit:

Quote:
Originally Posted by Germanator
Посмотреть сообщение
if(IsPlayerConnected(ReturnUser("xenia")))

That should work just fine.
Quote:
Originally Posted by Nuke547
Посмотреть сообщение
if(IsPlayerConnectedEx(id) == 1)
That didn't fix my problem


Re: How to check if player is online? - Germanator - 12.02.2012

Quote:
Originally Posted by Dr.Heinz
Посмотреть сообщение
i mean i want to check if xenia is online. and it is not possible to use IsPlayerConnected to check if she is online
Why not?
Use ReturnUser to get the ID and then do an IsPlayerConnected check.
As I posted above:
if(IsPlayerConnected(ReturnUser("xenia")))

ReturnUser gives you the ID of the player or INVALID_PLAYER_ID, if not connected.
And IsPlayerConnected checks if the person is connected.

Edit: The function "ReturnUser" is located in utils.inc, in case you didn't include it already.


Re: How to check if player is online? - Dr.Heinz - 12.02.2012

i get a error

Код:
C:\DOCUME~1\Heinz\MYDOCU~1\Heinz\GAMEMO~1\HE.pwn(1451) : error 017: undefined symbol "ReturnUser"
Pawn compiler 3.2.3664	 	 	Copyright © 1997-2006, ITB CompuPhase


1 Error.



Re: How to check if player is online? - Germanator - 12.02.2012

You need to include utils.inc into your script.
pawn Код:
#include <utils>
If you don't have it, just download it.

Edit: In case you're too lazy to download, here's the function itself copied from utils.inc:
pawn Код:
ReturnUser(text[], playerid = INVALID_PLAYER_ID)
{
    new pos = 0;
    while (text[pos] < 0x21) // Strip out leading spaces
    {
        if (text[pos] == 0) return INVALID_PLAYER_ID; // No passed text
        pos++;
    }
    new userid = INVALID_PLAYER_ID;
    if (IsNumeric(text[pos])) // Check whole passed string
    {
        // If they have a numeric name you have a problem (although names are checked on id failure)
        userid = strval(text[pos]);
        if (userid >=0 && userid < MAX_PLAYERS)
        {
            if(!IsPlayerConnected(userid))
            {
                /*if (playerid != INVALID_PLAYER_ID)
                {
                    SendClientMessage(playerid, 0xFF0000AA, "User not connected");
                }*/

                userid = INVALID_PLAYER_ID;
            }
            else
            {
                return userid; // A player was found
            }
        }
        /*else
        {
            if (playerid != INVALID_PLAYER_ID)
            {
                SendClientMessage(playerid, 0xFF0000AA, "Invalid user ID");
            }
            userid = INVALID_PLAYER_ID;
        }
        return userid;*/

        // Removed for fallthrough code
    }
    // They entered [part of] a name or the id search failed (check names just incase)
    new len = strlen(text[pos]);
    new count = 0;
    new name[MAX_PLAYER_NAME];
    for (new i = 0; i < MAX_PLAYERS; i++)
    {
        if (IsPlayerConnected(i))
        {
            GetPlayerName(i, name, sizeof (name));
            if (strcmp(name, text[pos], true, len) == 0) // Check segment of name
            {
                if (len == strlen(name)) // Exact match
                {
                    return i; // Return the exact player on an exact match
                    // Otherwise if there are two players:
                    // Me and MeYou any time you entered Me it would find both
                    // And never be able to return just Me's id
                }
                else // Partial match
                {
                    count++;
                    userid = i;
                }
            }
        }
    }
    if (count != 1)
    {
        if (playerid != INVALID_PLAYER_ID)
        {
            if (count)
            {
                SendClientMessage(playerid, 0xFF0000AA, "Multiple users found, please narrow earch");
            }
            else
            {
                SendClientMessage(playerid, 0xFF0000AA, "No matching user found");
            }
        }
        userid = INVALID_PLAYER_ID;
    }
    return userid; // INVALID_USER_ID for bad return
}



Re: How to check if player is online? - Dr.Heinz - 12.02.2012

i download it myself but. everytime i say i love you xenia yeah it works but the message Heinz has been sent

Код:
Xenia is not in the server!
Heinz: i love you xenia
pawn Код:
if(!strcmp(text, "i love you xenia", true))
    {
       if(!strcmp(pname, "Heinz", true))
       {
          if(IsPlayerConnected(ReturnUser("Xenia")))
          {
             format(string, sizeof(string), "Server-Owner %s has tell xenia i love you", pname);
             SendClientMessageToAll(COLOR_GREEN, string);
             printf(string);
             GameTextForPlayer(playerid, "~g~Sweet!", 2000, 3);
          }
          else return SendClientMessage(playerid, COLOR_RED, "Xenia is not in the server!");
          return 0;
       }
       else
       {
          format(string, sizeof(string), "%s(ID: %d) has been banned for telling xenia i love you", pname, playerid);
          SendClientMessageToAll(COLOR_RED, string);
          GameTextForPlayer(playerid, "~r~Shutup!", 2000, 3);
          BanEx(playerid, "Fuck-off");
       }
       return 0;
    }



Re: How to check if player is online? - Germanator - 12.02.2012

Remove the "return" you put before "SendClientMessage"
Just put "SendClientMessage" there.