[Tutorial] Killstreaks for players!
#1

Killstreaks


'Sup y'all? Today, I'm showing you how to script the famous killstreaks which many servers are including!

Requirements:
  • PAWN Basic knowledge
  • Your brain
1. Adding the global-variable:


First of all, add the global variable on the top of your script:

pawn Code:
new Streaks[MAX_PLAYERS];
It will store how many kills you currently have, for the maximum amount of players!

2. Resetting the variable:



Under the callbacks "OnPlayerConnect" and "OnPlayerDisconnect", you should add this line:


pawn Code:
Streaks[playerid] = 0;
Why? Usually, player won't have a streak when they'll leave or join the server! So we're giving the variable the value '0' when a player connects / disconnects, infact that it'll get reset again!

3. Increasing the variable's value:


Search for your callback "OnPlayerDeath"! Now, we're going to add a code which looks a little bigger, so I'm gonna continue step-by-step. First, we're going to add these lines, explained in comments:

pawn Code:
public OnPlayerDeath(playerid, killerid, reason)
{
if(IsPlayerConnected(killerid) &&  killerid != INVALID_PLAYER_ID ) { //check if the player is connected and it's a VALID KILL
        if(GetPlayerWantedLevel(killerid) < 6) //if the player's wantedlevel is under 6
          {
            SetPlayerWantedLevel(killerid, GetPlayerWantedLevel(killerid) + 1); //increase it by 1
          }
            Streaks[killerid] ++; //the sign '++' means, increase the variable for the killerid by 1
            GivePlayerMoney(killerid, 500); //if you like, give the killer some money, else comment this line out
        }
            SetPlayerWantedLevel(playerid, 0); //the player who died will get his wanted level reset to 0
        Streaks[playerid] = 0; //and his streak will end, while giving the value '0' to this variable
4. Switching through cases:


So now, we arrived at the main part of this tutorial! Under your code you currently have under "OnPlayerDeath", add the following:

pawn Code:
SetPlayerScore(killerid,GetPlayerScore(killerid)+1); //not really necessary, would increase the player's score by 1 point
        new str[ 256 ], KillerName[MAX_PLAYER_NAME]; //here, you're defining a string and the killername
        GetPlayerName(killerid, KillerName, sizeof(KillerName)); //receive the information of the killer's name
        switch(Streaks[killerid]) //IMPORTANT: with the function "switch", you're switching / toggling through the killstreaks of a player (Streals). you need the killerid here, because the playerid is the one which is GETTING killed
        {
            case 2: // we won't start with the first kill (case 1), because it wouldn't be countable as streak
            {
                format(str, sizeof(str), "[KILLINGSPREE] ~g~%s has performed a ~y~double kill!", KillerName); //here, you're formatting the defined string and add the content (text)
                GameTextForAll(str,4000,4); //this is a function, which will show a short, colored big text for all the players (str, 4000ms, stylenumber 4)
            }
            case 3:
            {
                format(str, sizeof(str), "[KILLINGSPREE] ~y~Triple Kill for ~b~%s!", KillerName);
                GameTextForAll(str,4000,4);
            }
            case 4:
            {
                format(str, sizeof(str), "[KILLINGSPREE] ~p~Quadro Kill for ~r~%s!", KillerName);
                GameTextForAll(str,4000,4);
            }
            case 5:
            {
                format(str, sizeof(str), "[KILLINGSPREE] ~r~%s is dominating with ~p~five kills!", KillerName);
                GameTextForAll(str,4000,4);
            }
            case 6:
            {
                format(str, sizeof(str), "[KILLINGSPREE] ~w~%s is godlike with ~y~six kills!", KillerName);
                    GameTextForAll(str,4000,4);
            }
            case 7:
            {
                format(str, sizeof(str), "[KILLINGSPREE] ~r~RAMPAGE for %s with ~w~seven kills!", KillerName);
                GameTextForAll(str,4000,4);
            }
            case 8:
            {
                format(str, sizeof(str), "[KILLINGSPREE] ~p~%s is unbelievable with ~y~eight kills!", KillerName);
                GameTextForAll(str,4000,4);
            }
            case 9:
            {
                format(str, sizeof(str), "[KILLINGSPREE] ~g~%s is worldclass, ~b~nine kills!", KillerName);
                GameTextForAll(str,4000,4);
            }
            case 10:
            {
                format(str, sizeof(str), "[KILLINGSPREE] ~b~%s is annihilating with ~g~ten kills!", KillerName);
                GameTextForAll(str,4000,4);
            }
/*
            case 11:
            {
            just continue however you want, just in the same way as above
            }*/
IMPORTANT NOTE:
The function "GameTextForAll" might not work 100% because it might getting mixed up with other gametext / textdraws! If it didn't work, use the following:

pawn Code:
SendClientMessageToAll(COLOR, str);
Credits:
- Tutorial and explanations - Twisted Insane
Reply
#2

that should be killerid not playerid
pawn Code:
GivePlayerMoney(killerid, 500); //if you like, give the player money, else comment this line out
nice tut.
Reply
#3

Useful TuT 4/5^^
Reply
#4

@emo

Fixed, you're totally right! And thanks for your feedback!

@Wickeed

Thank you!
Reply
#5

Awesome tutorial Twisted.

I will possibly learn a couple of things from this for when I develop my new gamemode for release.

Thank you once again!

pawn Code:
Rep[Twisted_Insane] += 1;
Reply
#6

A bit of redundant code, but otherwise nice. You must also assume that programmers are lazy. They don't like to type the same piece of code more than once.

pawn Code:
if(IsPlayerConnected(killerid) &&  killerid != INVALID_PLAYER_ID )
Is redundant. If the killerid is connected, then it is obviously not invalid and vice versa.

Second, the switch statement can be simplified:
pawn Code:
switch(Streaks[killerid]) //IMPORTANT: with the function "switch", you're switching / toggling through the killstreaks of a player (Streals). you need the killerid here, because the playerid is the one which is GETTING killed
{
    case 2: format(str, sizeof(str), "[KILLINGSPREE] ~g~%s has performed a ~y~double kill!", KillerName);
    case 3: format(str, sizeof(str), "[KILLINGSPREE] ~y~Triple Kill for ~b~%s!", KillerName);
    case 4: format(str, sizeof(str), "[KILLINGSPREE] ~p~Quadro Kill for ~r~%s!", KillerName);
    // etc ..
}
GameTextForAll(str,4000,4); //this is a function, which will show a short, colored big text for all the players (str, 4000ms, stylenumber 4)
Reply
#7

@iGetty

Aw, thank you! That's nice from ya, you could also give me some ideas for the next tutorial! Things which could be (maybe) useful for you and many others. ^^

@Vince

Hmm, I also just thought about this, lol! So it simply could be done with this line?

pawn Code:
if(killerid != INVALID_PLAYER_ID ) {
To the switching:

DIDN'T EVEN KNOW THAT! I might correct it right now, it's way more simple, just as you said, you've taught me something new. ^^
Reply
#8

I sure will indeed Twisted!

Let me think some up and I will PM you with my idea's

Thanks!
Reply
#9

@iGetty

You're welcome and thanks! ^^
Reply
#10

Quote:
Originally Posted by Twisted_Insane
View Post
To the switching:

DIDN'T EVEN KNOW THAT! I might correct it right now, it's way more simple, just as you said, you've taught me something new. ^^
A switch statement is much more efficient than an if statement. Nice tutorial, though.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)