08.03.2015, 12:45
(
Last edited by Sellize; 09/03/2015 at 05:49 PM.
Reason: Added tutorial (posted too soon)
)

You will be shown how to make a fairly basic hitman system. This script will cover: placing hits on players (/contract) and executing contracts (hitmen)

Do you need a newbie tutorial? Take a look at this tutorial.
Required: zcmd, sscanf
Let's start by creating a new blank script, or if you want to incorporate this into your current script, that's possible too!
With the help of ZCMD I'll go ahead and create two commands: /contract and /hits
/contract [id] [amount] - Players will be able to use this command to place hits on other players
/hits - Hitmen can use this command to see all current hits
Code:
COMMAND:contract(playerid, params[]) { return 1; } COMMAND:hits(playerid, params[]) { return 1; }
hit - This variable will store the hit amount on the player
isHitman - This variable is to declare if a player is a hitman or not 0 = no, 1 = yes.
Code:
new hit[MAX_PLAYERS]; new isHitman[MAX_PLAYERS];
Let's work on the /contract command.
We first need to see if the player actually used the parameters correctly. We use sscanf to check these for us.
Code:
COMMAND:contract(playerid, params[]) { new id, amount; if(sscanf(params, "ui", id, amount)) return SendClientMessage(playerid, -1, "{A3A1A0}Usage: /contract [id] [amount]"); return 1; }
Next up we need to check if the specified player is actually connected. We can use the IsPlayerConnected function for that. If the player is not connected we will return an error message.
Code:
COMMAND:contract(playerid, params[]) { new id, amount; if(sscanf(params, "ui", id, amount)) return SendClientMessage(playerid, -1, "{A3A1A0}Usage: /contract [id] [amount]"); if(!IsPlayerConnected(id)) return SendClientMessage(playerid, -1, "{A3A1A0}That player is not connected."); return 1; }
Code:
if(isHitman[playerid] == 1) return SendClientMessage(playerid, -1, "{A3A1A0}Hitmen can not place contracts.");
Code:
if(isHitman[id] == 1) return SendClientMessage(playerid, -1, "{A3A1A0}That player already has the maximum hit on him.");
Now we will check if the player already has the maximum hit, which will be $150k.
Code:
if(hit[id] == 150000 || hit[id] > 150000) return SendClientMessage(playerid, -1, "{A3A1A0}That player already has the maximum hit on him.");
Code:
if(amount < 50000 || amount > 150000) return SendClientMessage(playerid, -1, "{A3A1A0}Hits range from $50,000 - $150,000.");
Code:
if(hit[id] + amount > 150000) return SendClientMessage(playerid, -1, "{A3A1A0}That hit is too high for this player, please lower it.");
Go ahead and add the amount to the current hit of the player.
Code:
hit[id] = hit[id] + amount;
We will first make two strings, one for the finished message and one to store the targets name in.
Then we can format the finished string and finally send it to the player using SendClientMessage
Code:
new result[128]; new tname[MAX_PLAYER_NAME]; GetPlayerName(id, tname, sizeof(tname)); format(result, sizeof(result), "You have placed a hit of $%i on %s.", amount, tname); SendClientMessage(playerid, -1, result);
We will use a for loop for this. It will loop through all online players and check if they are a hitman, if they are, then we will send them the message!
Make sure to use 'i' instead of 'playerid' here!
Code:
new string[128]; format(string, sizeof(string), "HITMAN: A new hit is available. Name: %s | Amount: $%i", tname, hit[id]); for(new i = 0; i < MAX_PLAYERS; i++) { if(IsPlayerConnected(i)) { if(isHitman[i]) { SendClientMessage(i, -1, string); } } }
PHP Code:
COMMAND:contract(playerid, params[])
{
new id, amount;
if(sscanf(params, "ui", id, amount)) return SendClientMessage(playerid, -1, "{A3A1A0}Usage: /contract [id] [amount]");
if(!IsPlayerConnected(id)) return SendClientMessage(playerid, -1, "{A3A1A0}That player is not connected.");
if(isHitman[playerid] == 1) return SendClientMessage(playerid, -1, "{A3A1A0}Hitmen can not place contracts.");
if(isHitman[id] == 1) return SendClientMessage(playerid, -1, "{A3A1A0}That player already has the maximum hit on him.");
if(hit[id] == 150000 || hit[id] > 150000) return SendClientMessage(playerid, -1, "{A3A1A0}That player already has the maximum hit on him.");
if(amount < 50000 || amount > 150000) return SendClientMessage(playerid, -1, "{A3A1A0}Hits range from $50,000 - $150,000.");
if(hit[id] + amount > 150000) return SendClientMessage(playerid, -1, "{A3A1A0}That hit is too high for this player, please lower it.");
hit[id] = hit[id] + amount;
new result[128];
new tname[MAX_PLAYER_NAME];
GetPlayerName(id, tname, sizeof(tname));
format(result, sizeof(result), "You have placed a hit of $%i on %s.", amount, tname);
SendClientMessage(playerid, -1, result);
new string[128];
format(string, sizeof(string), "HITMAN: A new hit is available. Name: %s | Amount: $%i", tname, hit[id]);
for(new i = 0; i < MAX_PLAYERS; i++)
{
if(IsPlayerConnected(i))
{
if(isHitman[i])
{
SendClientMessage(i, -1, string);
}
}
}
return 1;
}
Go to the OnPlayerDeath callback.
First we check if the killerid of the death exists using:
Code:
if(killerid != INVALID_PLAYER_ID) {
Code:
if(isHitman[killerid] == 1) { }
If the player that died had a hit on him, we will give the hitman the amount of money of the hit and we will send him a message.
We will also clear the hit on the player.
This should be your final OnPlayerDeath callback
PHP Code:
public OnPlayerDeath(playerid, killerid, reason)
{
if(killerid != INVALID_PLAYER_ID)
{
if(isHitman[killerid] == 1)
{
if(hit[playerid] > 0)
{
GivePlayerMoney(killerid, hit[playerid]);
new result[128];
new tname[MAX_PLAYER_NAME];
GetPlayerName(playerid, tname, sizeof(tname));
format(result, sizeof(result), "HITMAN: You killed %s and collected the hit of $%i!", tname, hit[playerid]);
SendClientMessage(playerid, -1, result);
hit[playerid] = 0;
}
}
}
return 1;
}
Then we will again use a for loop to loop through all players and check if their hit is higher that 0, if it is, we will send a message with their information. Again using format and strings.
This should be your final /hits command:
PHP Code:
COMMAND:hits(playerid, params[])
{
if(isHitman[playerid] == 1)
{
SendClientMessage(playerid, -1, "--- Current available contracts ---");
new string[128];
new tname[MAX_PLAYER_NAME];
for(new i = 0; i < MAX_PLAYERS; i++)
{
if(IsPlayerConnected(i))
{
if(hit[i] > 0)
{
format(string, sizeof(string), "Name: %s | Amount: %i", tname, hit[i]);
SendClientMessage(playerid, -1, string);
}
}
}
}
return 1;
}

There are things which I could not show in this tutorial, these things are your responsibility however.
Think about: saving hits, making people hitman etc.
This shouldn't be too hard to do!
You can download the .PWN file from the attachment or view the script on the pastebin link.