SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help > Tutorials

Reply
 
Thread Tools Display Modes
Old 11/01/2017, 05:35 PM   #1
iLearner
Guest
 
Posts: n/a
Default HeadShot System [Explained]

Tutorial - HeadShot System



Intro
Nothing special, just a small tutorial explaining about how to make a "HeadShot" system, i do not claim this as effort, since its pretty easy & quick... But since i could not find any decent tutorial i thought... lets do dis.


Content

  • Explaining OnPlayerTakeDamage, since we're going to need it.
  • HeadShot system mini-script.
  • Team Protection (cant hit same team).
  • Enable/Disable HeadShot system.
  • Anti-HeadShot spam(looks weired?)



Let's Get Started

For this tutorial we're going to base on callback OnPlayerTakeDamage.

Description according to wiki page:
Quote:
This callback is called when a player takes damage.

That one explains pretty much everything... but lets look at it deeply with its parameters:
PHP Code:
public OnPlayerTakeDamage(playeridissueridFloat:amountweaponidbodypart
  • playerid - player who took damage.
  • issuerid - Player who gave damage(The Shooter), can be INVALID_PLAYER_ID if the damage was caused by self.
  • Float:amount - Amount in Float, of much damage did player took (not needed in our case actually, but you could use it for other things... such as labels on head "Damage -amount")
  • weaponid - ID of the weapon that was used while giving damage.
  • bodypart - The bodypart where player got hit (0.3z+)



Code
We will need a global variable called "HeadShotSystem", so lets define it, anywhere outside a callback/function (near includes?)

PHP Code:
booleanserver variable not player variable
new bool:HeadShotSystem
Main Code

PHP Code:
public OnPlayerTakeDamage(playeridissueridFloat:amountweaponidbodypart)
{    
    
// check if there was a shooter, weaponID was 34(Sniper) and bodypart was 9(Head)
    
if(issuerid != INVALID_PLAYER_ID && weaponid == 34 && bodypart == 9)
    {
        
//checking if headshot system is ON.
        
if(HeadShotSystem == true)
        {
            
// checks if both players have same team, except NO_TEAM. if not procced, otherwise send error message
//*A edit by our genius friend sickattack
            
if(GetPlayerTeam(issuerid) | GetPlayerTeam(playerid) == NO_TEAM || GetPlayerTeam(issuerid) != GetPlayerTeam(playerid)) 
            {    
//check if player is dead... if not proceed(by Gammix)
                
if (GetPlayerState(playerid) != PLAYER_STATE_WASTED)
                {
                    
//variables, first one to format the message, second and third contain player names.
                    
new headmsg[128], dead[24], killer[24];
                    
//on player's screen we show him a message for 3 seconds "HeadShot"
                    
GameTextForPlayer(playerid"~r~Head Shot",3000,4);
                    
// same to issuer's screen
                    
GameTextForPlayer(issuerid"~r~Head Shot!",3000,4);
                    
// we get the victims name with this function and store it into our previously made variable "dead";
                    
GetPlayerName(playeriddeadsizeof(dead));
                    
// we get the victims name with this function and store it into our previously made variable "killer";
                    
GetPlayerName(issueridkillersizeof(killer));
                    
//format the message, means we put that text into "headmsg".
                    
format(headmsgsizeof(headmsg), "{FFDC2E}%s (%i) Has Been Killed in a Headshot by %s (%i)!",deadplayeridkiller,issuerid);
                    
// once we've formatted the message we're ready to send the message to all players!
                    
SendClientMessageToAll(0xAA3333AAheadmsg);
                    
//kill the player
                    
SetPlayerHealth(playerid0.0);
                    
//and tell the server that he's dead!
                
}
            }
        }
        else
            
SendClientMessage(issuerid, -1"That player is in your team!");        
    }
    return 
1;


Command to enable/disable headshot system

*Optimized by Sreyas
PHP Code:
COMMAND:seths(playeridparams[]) 

        
//replace with your admin system 
        
if(IsPlayerAdmin(playerid)) 
        { 

            
HeadShotSystem = !HeadShotSystem
            new 
string11[109], pname[24]; 
            
GetPlayerName(playeridpname24); 
            
format(string11sizeof(string11), "[Admin] Server admin %s(%d) has %s Headshot system!"pname,playerid,(HeadShotSystem)?("enabled"):("disabled")); 
            
SendClientMessageToAll(-1string11); 

        } 
        return 
1





Requirements:
- SA:MP version 0.3z +(bodypart)
- sscanf (for the cmd we've used)
- A IDE


Credits:
- SA:MP Team.
- y_less for sscanf



Did i miss something, lemme know with a post, and remember! this is a shit tutorial!

Last edited by iLearner; 12/01/2017 at 02:26 PM.
  Reply With Quote
Old 11/01/2017, 05:43 PM   #2
Gammix
High-roller
 
Gammix's Avatar
 
Join Date: Jan 2015
Location: Ottawa, Canada
Posts: 1,535
Reputation: 586
Default Re: HeadShot System [Explained]

PHP Code:
stock IsPlayerDead(playerid)
{
    new 
Floathealth 0.0;
    
GetPlayerHealth(playeridhealth);
    if(
GetPlayerState(playerid) == PLAYER_STATE_WASTED && health <= 0.0
        return 
true;
    return 
false;

You don't need "playerDead" variable.



The right way for checking teams aren't equal:
PHP Code:
if(GetPlayerTeam(issuerid) == NO_TEAM || GetPlayerTeam(playerid) == NO_TEAM || GetPlayerTeam(issuerid) != GetPlayerTeam(playerid)) 
__________________

Howz it going?!


Gammix is offline   Reply With Quote
Old 11/01/2017, 05:44 PM   #3
Logic_
High-roller
 
Logic_'s Avatar
 
Join Date: Jun 2015
Posts: 1,196
Reputation: 120
Default Re: HeadShot System [Explained]

OPDeath callback...
Logic_ is offline   Reply With Quote
Old 11/01/2017, 05:45 PM   #4
iLearner
Guest
 
Posts: n/a
Default Re: HeadShot System [Explained]

Edited with those changes, thanks.
  Reply With Quote
Old 11/01/2017, 05:54 PM   #5
RedRex
Huge Clucker
 
RedRex's Avatar
 
Join Date: Jan 2016
Posts: 376
Reputation: 22
Default Re: HeadShot System [Explained]

Good Tutoiral
__________________
Ninja's Cops and Robbers (Soon)
RedRex is offline   Reply With Quote
Old 11/01/2017, 06:02 PM   #6
saffierr
High-roller
 
saffierr's Avatar
 
Join Date: Jul 2013
Location: GetPlayerGirlHouse(targetplayer);
Posts: 1,081
Reputation: 32
Default Re: HeadShot System [Explained]

Nice tutorial, though I'm a bit confused with this line (Just the first two checks):
PHP Code:
if(GetPlayerTeam(issuerid) == NO_TEAM || GetPlayerTeam(playerid) == NO_TEAM 
Can you elaborate this ?

And is NO_TEAM already defined by pawn its self?

Excuse me, but haven't worked with this before, hence the question.
saffierr is offline   Reply With Quote
Old 11/01/2017, 06:05 PM   #7
Logic_
High-roller
 
Logic_'s Avatar
 
Join Date: Jun 2015
Posts: 1,196
Reputation: 120
Default Re: HeadShot System [Explained]

NO_TEAM is pre-defined by the main SA-MP include. The value of NO_TEAM is 255. There is a large difference between Pawn and SA-MP Pawn.
Logic_ is offline   Reply With Quote
Old 11/01/2017, 06:06 PM   #8
iLearner
Guest
 
Posts: n/a
Default Re: HeadShot System [Explained]

Quote:
Originally Posted by saffierr View Post
Nice tutorial, though I'm a bit confused with this line (Just the first two checks):
PHP Code:
if(GetPlayerTeam(issuerid) == NO_TEAM || GetPlayerTeam(playerid) == NO_TEAM 
Can you elaborate this ?

And is NO_TEAM already defined by pawn its self?

Excuse me, but haven't worked with this before, hence the question.

PHP Code:
if(GetPlayerTeam(issuerid) == NO_TEAM || GetPlayerTeam(playerid) == NO_TEAM || GetPlayerTeam(issuerid) != GetPlayerTeam(playerid)) 
It basically means, if both players have no team, then they can kill each other (typically freeroam / dm servers), OR if both players' teams are different (eg: playerid team: 3, issuerid team:2), then proceed, otherwise nope.

And yes, NO_TEAM is pre-defined just like MAX_PLAYERS.
  Reply With Quote
Old 11/01/2017, 06:08 PM   #9
saffierr
High-roller
 
saffierr's Avatar
 
Join Date: Jul 2013
Location: GetPlayerGirlHouse(targetplayer);
Posts: 1,081
Reputation: 32
Default Re: HeadShot System [Explained]

Ah, alright I see, thanks both of you.

Keep up the good work, iLearn!

Edit: Wouldn't it be better to have the 'HeadShotSystem check' before the check above?
saffierr is offline   Reply With Quote
Old 11/01/2017, 06:32 PM   #10
RyderX
Banned
 
Join Date: Oct 2016
Location: Lebanon,Beirut Rank: SA-MP Scripter
Posts: 603
Reputation: 53
Default Re: HeadShot System [Explained]

nice work iLearner :{}
RyderX is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
HELP in headshot system VaisH Scripting Help 4 25/12/2015 06:32 PM
[Tutorial] Login+Register System / Strcmp and ZCMD Commands / Random Messages - Explained ! Giroud12 Tutorials 17 11/08/2013 03:18 AM
[Tutorial] How To Make A Nuclear System | For TDM | DM | Explained! Nirzor Tutorials 11 01/11/2012 06:31 AM
HeadShot system NeyMar96 Scripting Help 5 29/08/2011 07:59 PM


All times are GMT. The time now is 11:16 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.