SA-MP Forums Archive
[Tutorial] Killstreaks for players! - 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)
+---- Forum: Tutorials (https://sampforum.blast.hk/forumdisplay.php?fid=70)
+---- Thread: [Tutorial] Killstreaks for players! (/showthread.php?tid=328341)



Killstreaks for players! - Twisted_Insane - 24.03.2012

Killstreaks


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

Requirements:
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


Re: Killstreaks for players! - emokidx - 25.03.2012

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.


Re: Killstreaks for players! - Wickeed - 25.03.2012

Useful TuT 4/5^^


Re: Killstreaks for players! - Twisted_Insane - 25.03.2012

@emo

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

@Wickeed

Thank you!


Re: Killstreaks for players! - iGetty - 25.03.2012

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;



Re: Killstreaks for players! - Vince - 25.03.2012

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)



Re: Killstreaks for players! - Twisted_Insane - 25.03.2012

@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. ^^


Re: Killstreaks for players! - iGetty - 25.03.2012

I sure will indeed Twisted!

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

Thanks!


Re: Killstreaks for players! - Twisted_Insane - 25.03.2012

@iGetty

You're welcome and thanks! ^^


Re: Killstreaks for players! - 2KY - 25.03.2012

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.