Join and leave messages

I am going to show you how to make some join and leave messages for your server, all this does is send a message out to all players on your server when someone connects or disconnects.


First off we need to add the arrays to hold the player name and the message itself. Im going to name mine "pName" for "player name" and "str" for "string" which basically means a line of text. You will need to add these in both "OnPlayerConnect" and "OnPlayerDisconnect". Mine now looks like this:

public OnPlayerConnect(playerid)
{
    new pName[24];
    new str[128];
    return 1;
}

public OnPlayerDisconnect(playerid, reason)
{
    new pName[24];
    new str[128];
    return 1;
}

Next we need to get the players name, use "GetPlayerName". The parameters are "playerid, const name[], len", we can put in "playerid, pName, 24".

public OnPlayerConnect(playerid)
{
    new pName[24];
    new str[128];
    GetPlayerName(playerid, pName, 24);
    return 1;
}

public OnPlayerDisconnect(playerid, reason)
{
    new pName[24];
    new str[128];
    GetPlayerName(playerid, pName, 24);
    return 1;
}

Next we have to add the message part to "OnPlayerConnect". Because the message will have the players name in it we need to use "format", mine looks like this "format(str, 128, "%s has joined the server", pName);" This will say "weedarr has left the server". We also need to actually send the message to the players so use something like this "SendClientMessageToAll(0xAFAFAFAA, str);". My callback now looks like this:

public OnPlayerConnect(playerid)
{
    new pName[24];
    new str[128];
    GetPlayerName(playerid, pName, 24);
    format(str, 128, "%s has joined the server", pName);
    SendClientMessageToAll(0xAFAFAFAA, str);
    return 1;
}

That's the first callback done, now we move on to the second, this requires a bit more code than before due to the 3 different reason the player could have left because of. You can copy and paste the code from the "OnPlayerConnect" callback but delete the format line. It should look like this:

public OnPlayerDisconnect(playerid, reason)
{
    new pName[24];
    new str[128];
    GetPlayerName(playerid, pName, 24);

    SendClientMessageToAll(0xAFAFAFAA, str);
    return 1;
}

Next we must use a switch statement to determine why the player left the server.

public OnPlayerDisconnect(playerid, reason)
{
    new pName[24];
    new str[128];
    GetPlayerName(playerid, pName, 24);

    switch(reason)
        {
            case 0: format(str, 128, "%s has left. (Timeout)", pName);
            case 1: format(str, 128, "%s has left. (Leaving)", pName);
            case 2: format(str, 128, "%s has left. (Kicked)", pName);
        }
    SendClientMessageToAll(0xAFAFAFAA, str);
    return 1;
}

Thats that bit done. The 2 callbacks should now look like this:

public OnPlayerConnect(playerid)
{
    new pName[24];
    new str[128];
    GetPlayerName(playerid, pName, 24);
    format(str, 128, "%s has joined the server", pName);
    SendClientMessageToAll(0xAFAFAFAA, str);
    return 1;
}

public OnPlayerDisconnect(playerid, reason)
{
    new pName[24];
    new str[128];
    GetPlayerName(playerid, pName, 24);

    switch(reason)
        {
            case 0: format(str, 128, "%s has left. (Timeout)", pName);
            case 1: format(str, 128, "%s has left. (Leaving)", pName);
            case 2: format(str, 128, "%s has left. (Kicked)", pName);
        }
    SendClientMessageToAll(0xAFAFAFAA, str);
    return 1;
}

Thats it done.


Back | Home