[Tutorial] Make your own team deathmatch with SetPlayerTeam
#1

Hi peoples, in this tutorial I will teach you how to make your own Team deathmatch ( TDM ) gamemode.

1. Introduction
- What is Team Deathmatch?

It is some kind of a deathmatch, but with teams!

2. Requirements
1. A basic PAWN knowledge.

3. Creating the gamemode
Open your pawno program, click CTRL+A on your keyboard ( Select all ) and delete it.

Add the following code :

pawn Код:
#include    < a_samp >
We are including a_samp, the include itself also includes some other files, which will help us ( Some includes got an useful functions ).

Next, we will define team ID's
pawn Код:
#define TEAM_BALLAS ( 0 ) // Defines TEAM_BALLAS as team ID 0
#define TEAM_CJ ( 1 ) // Defines TEAM_CJ as team ID 1
Below it add :

pawn Код:
main( )
- What is that thing?

As far as I know, it's a callback which starts after OnGameModeInit, and AFAIK it won't be called on a filterscript ( #define FILTERSCRIPT )


pawn Код:
main( )
{ // Opening bracket
    print( ">> Team deathmatch by < yourname > has been loaded!" );
} // Closing bracket
print is a function which will sends a message on the server console.

And add this :
pawn Код:
public OnGameModeInit( ) // This callback is called when the gamemode is started.
{
    return 1; // This means, the gamemode is processed succesfully AFAIK.
}
3.1. Adding classes
Quote:

You will probably want to move some of your spawn points about so everyone doesn't spawn in the same place, which would make a rather boring deathmatch and camping amazingly easy. This is where the debug mode comes in. Run "samp_debug.exe" in your San Andreas root directory and whenever you want to save a co-ordinate type "/save" in the game (press "t" or the key under escape to bring up the text/chat prompt). This will save your current position, angle and playerid to a file called "savedpositions.txt" in your Documents/GTA SA Userfiles/savedpositions.txt

Quoted from SA-MP wiki.

Let's continue!

Add the classes ( from savedpositions.txt ) below OnGameModeInit
pawn Код:
public OnGameModeInit( )
{
    AddPlayerClass( 102, 1958.3783, 1343.1572, 15.3746, 269.1425, 5, 0, 22, 100, 32, 50 ); // Ballas
    // The class above is classid 0.

    AddPlayerClass( 0, 1958.3783, 1343.1572, 15.3746, 269.1425, 0, 0, 0, 0, 0, 0 ); // CJ
    // Multiply the classid + 1, so this class is classid 1.

    AddPlayerClass( 1, 1958.3783, 1343.1572, 15.3746, 269.1425, 0, 0, 0, 0, 0, 0 ); // The truth
    // Multiply the classid + 1 again, so this is classid 2.

    return 1;
}
AddPlayerClass will add a class, this is the arguments :
pawn Код:
AddPlayerClass( skinID, Float: X, Float: Y, Float: Z, Float: A, Weapon 0, Ammo 0, Weapon 1, Ammo 1, Weapon 2, Ammo 2 );
.

Let's continue again.

Now we will add a callback which will calls when a player is on class selection.

pawn Код:
public OnPlayerRequestClass( playerid, classid ) //  This is called when a player is on class selection
{
    return 1; // This means the callback is executed succesfully AFAIK.
}
We will add gametexts!

pawn Код:
public OnPlayerRequestClass( playerid, classid ) //  This is called when a player is on class selection
{
    switch ( classid ) // This is like a if/elseif statement.
    {
        case 0: // If the classid is 0
        { // Then
            GameTextForPlayer( playerid, "~r~TEAM Ballas", 300, 3 );
            // Sends a gametext for player
            // ~r~ is a red color in gametext.
            // Arguments : Player ID, const string[ ], time ( milliseconds ), style
            // Wiki for more info
            SetPlayerTeam( playerid, TEAM_BALLAS ); // Sets the player's team to TEAM_BALLAS
        }

        case 1: // If the classid is 1
        { // Then
            GameTextForPlayer( playerid, "~g~TEAM CJ", 300, 3 );
            // Sends a gametext for player
            // ~g~ is a green color in gametext.
            // Arguments : Player ID, const string[ ], time ( milliseconds ), style
            // Wiki for more info
            SetPlayerTeam( playerid, TEAM_CJ ); // Sets the player's team to TEAM_CJ
        }

        case 2: // If the classid is 2
        { // Then
            GameTextForPlayer( playerid, "~g~TEAM CJ", 300, 3 );
            // Sends a gametext for player
            // ~g~ is a green color in gametext.
            // Arguments : Player ID, const string[ ], time ( milliseconds ), style
            // Wiki for more info
            SetPlayerTeam( playerid, TEAM_CJ ); // Sets the player's team to TEAM_CJ
        }
    }    
    return 1; // This means the callback is executed succesfully AFAIK.
}
Let's continue.

We will add an anti-teamkill! ( With SetPlayerTeam they can still be killed with knifes. )

pawn Код:
public OnPlayerDeath( playerid, killerid, reason ) // This is called when a player deaths.
{
    if ( killerid != INVALID_PLAYER_ID ) // If the killerid are NOT an invalid player
    { // Then
        if ( GetPlayerTeam( killerid ) == GetPlayerTeam( playerid ) ) // If killerid's team == playerid's team ( who has been killed )
        { // Then
            SetPlayerHealth( killerid, -1 ); // Kills the killer ( Setting the health to -1 ).

            SendClientMessage( killerid, -1, "No teamkilling." ); // Sends a message to the killer with a white colour ( -1 ), with the message " No teamkilling. ".

            GivePlayerMoney( killerid, - 5000 ); // Take $5000 from the killerid, because teamkilling,
        }

        else // Another team killed the player
        {
            SendClientMessage( killerid, -1, "Nice shot!" ); // Sends a message to the killer with a white colour ( -1 ) with the message "Nice Shot! ".

            GivePlayerMoney( killerid, 5000 ); // Gives the killerid $5000.
        }
    }

    SendDeathMessage(  killerid, playerid, reason ); // Sends a death message about the killerid is killing the player, with the reason ( At the middle right of your game, can be toggled by pressing F9 ).

    return 1; // This means the callback is executed succesfully AFAIK.
}
The whole gamemode should looks like :
pawn Код:
#include    < a_samp >

#define TEAM_BALLAS ( 0 ) // Defines TEAM_BALLAS as team ID 0
#define TEAM_CJ ( 1 ) // Defines TEAM_CJ as team ID 1

main( )
{ // Opening bracket
    print( ">> Team deathmatch by < yourname > has been loaded!" );
} // Closing bracket

public OnGameModeInit( )
{
    AddPlayerClass( 102, 1958.3783, 1343.1572, 15.3746, 269.1425, 5, 0, 22, 100, 32, 50 ); // Ballas
    // The class above is classid 0.

    AddPlayerClass( 0, 1958.3783, 1343.1572, 15.3746, 269.1425, 0, 0, 0, 0, 0, 0 ); // CJ
    // Multiply the classid + 1, so this class is classid 1.

    AddPlayerClass( 1, 1958.3783, 1343.1572, 15.3746, 269.1425, 0, 0, 0, 0, 0, 0 ); // The truth
    // Multiply the classid + 1 again, so this is classid 2.

    return 1;
}

public OnPlayerRequestClass( playerid, classid ) //  This is called when a player is on class selection
{
    switch ( classid ) // This is like a if/elseif statement.
    {
        case 0: // If the classid is 0
        { // Then
            GameTextForPlayer( playerid, "~r~TEAM Ballas", 300, 3 );
            // Sends a gametext for player
            // ~r~ is a red color in gametext.
            // Arguments : Player ID, const string[ ], time ( milliseconds ), style
            // Wiki for more info
            SetPlayerTeam( playerid, TEAM_BALLAS ); // Sets the player's team to TEAM_BALLAS
        }

        case 1: // If the classid is 1
        { // Then
            GameTextForPlayer( playerid, "~g~TEAM CJ", 300, 3 );
            // Sends a gametext for player
            // ~g~ is a green color in gametext.
            // Arguments : Player ID, const string[ ], time ( milliseconds ), style
            // Wiki for more info
            SetPlayerTeam( playerid, TEAM_CJ ); // Sets the player's team to TEAM_CJ
        }

        case 2: // If the classid is 2
        { // Then
            GameTextForPlayer( playerid, "~g~TEAM CJ", 300, 3 );
            // Sends a gametext for player
            // ~g~ is a green color in gametext.
            // Arguments : Player ID, const string[ ], time ( milliseconds ), style
            // Wiki for more info
            SetPlayerTeam( playerid, TEAM_CJ ); // Sets the player's team to TEAM_CJ
        }
    }
    return 1; // This means the callback is executed succesfully AFAIK.
}

public OnPlayerDeath( playerid, killerid, reason ) // This is called when a player deaths.
{
    if ( killerid != INVALID_PLAYER_ID ) // If the killerid are NOT an invalid player
    { // Then
        if ( GetPlayerTeam( killerid ) == GetPlayerTeam( playerid ) ) // If killerid's team == playerid's team ( who has been killed )
        { // Then
            SetPlayerHealth( killerid, -1 ); // Kills the killer ( Setting the health to -1 ).

            SendClientMessage( killerid, -1, "No teamkilling." ); // Sends a message to the killer with a white colour ( -1 ), with the message " No teamkilling. ".

            GivePlayerMoney( killerid, - 5000 ); // Take $5000 from the killerid, because teamkilling,
        }

        else // Another team killed the player
        {
            SendClientMessage( killerid, -1, "Nice shot!" ); // Sends a message to the killer with a white colour ( -1 ) with the message "Nice Shot! ".

            GivePlayerMoney( killerid, 5000 ); // Gives the killerid $5000.
        }
    }

    SendDeathMessage(  killerid, playerid, reason ); // Sends a death message about the killerid is killing the player, with the reason ( At the middle right of your game, can be toggled by pressing F9 ).

    return 1; // This means the callback is executed succesfully AFAIK.
}
That's all I can bring, because I had a homework ATM.

Hope it can be useful for newbies!

p.s. This is my first tutorial I have ever made.
Reply
#2

Not bad, Good Job. Explain more on Functions.

+Rep.
Reply
#3

Thanks for the quick reply, I will explain more ASAP, because I am busy right now.
Reply
#4

Can be very useful, nice tutorial.
Reply
#5

SetPlayerTeam can be buggy, I just noticed myself. I tried to implement anti-teamkill but it wouldn't work half of the time for some players that attempt team killing, overall nice.
Reply
#6

Quote:
Originally Posted by Lorenc_
Посмотреть сообщение
SetPlayerTeam can be buggy, I just noticed myself. I tried to implement anti-teamkill but it wouldn't work half of the time for some players that attempt team killing, overall nice.
Agreed. I myself experienced problems with SetPlayerTeam. It would be best to simply create a variable which would represent each team.
Reply
#7

SetPlayerTeam is not very good for anti-teamkill as everyone has said..

nice tut,, it would help newbies
Reply
#8

good and explained. 5/5
Reply
#9

Very nice, thanks but I have a problem: I want to have an anti teamkill in Los Santos but I don't want to have an anti tk on my /dm, special place like area 59 etc.. How to do that ?
Reply
#10

nice tut but i have a problem




E:\grand theft auto\server\gamemodes\gangz.pwn(72) : error 040: duplicate "case" label (value 4)
Pawn compiler 3.2.3664 Copyright © 1997-2006, ITB CompuPhase


1 Error.
Reply
#11

nice tutorial.....
MANTAP GAN, TERUSKAN....
INDONESIA!!!!!
Reply
#12

Makasih gan, gue indonesia jga agan

nice one bro,
Reply
#13

It's nice, however, I don't recommend everyone to cancel everything from their Pawno file. Simply because this can easily be transferred to a current script.
Reply
#14

Sorry for bumping this everyone, I have a question if someone could help me with it that would be great.

Quote:

AddPlayerClass( 102, 1958.3783, 1343.1572, 15.3746, 269.1425, 5, 0, 22, 100, 32, 50 ); // Ballas

How do I know it's the Team Ballas and not CJ, I didn't understand...
Reply
#15

This helped me a lot in learning pawn thanks bro
Reply
#16

The players of same team can't kill each other.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)