Infection
#1

I was trying to making a infection so heres what got and my problem is that when u press key_fire u lose hp
how could i make it so if u punch the player u punch they lose hp
Example: A noob punchs a mafia the mafia loses hp

pawn Код:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    if(PRESSED(KEY_FIRE)) {
        if(GetPlayerWeapon(playerid) == 4) {
            new Float:health;
            GetPlayerHealth(playerid, health);
            SetPlayerHealth(playerid, health - 50.0);
            return 1;
        }
    }
    return 1;
}
Reply
#2

You might have to loop through all the players to see which is closest to the player using the function, then take health from that player. Thats the first thing that comes to mind for me. You'd have to do other checks to make it bug free like make sure the target is in front of the player and make sure they are close enough ect, ect.
Reply
#3

Solved
Reply
#4

pawn Код:
forward Float:GetDistanceBetweenPlayers(p1,p2); // Up top of script

forward GetClosestPlayer(p1); // Up top of script

public Float:GetDistanceBetweenPlayers(p1,p2)
{
    new Float:x1,Float:y1,Float:z1,Float:x2,Float:y2,Float:z2;
    if(!IsPlayerConnected(p1) || !IsPlayerConnected(p2))
    {
        return -1.00;
    }
    GetPlayerPos(p1,x1,y1,z1);
    GetPlayerPos(p2,x2,y2,z2);
    return floatsqroot(floatpower(floatabs(floatsub(x2,x1)),2)+floatpower(floatabs(floatsub(y2,y1)),2)+floatpower(floatabs(floatsub(z2,z1)),2));
}

public GetClosestPlayer(p1)
{
    new x,Float:dis,Float:dis2,player;
    player = -1;
    dis = 99999.99;
    for (x=0;x<MAX_PLAYERS;x++)
    {
        if(IsPlayerConnected(x))
        {
            if(x != p1)
            {
                dis2 = GetDistanceBetweenPlayers(x,p1);
                if(dis2 < dis && dis2 != -1.00)
                {
                    dis = dis2;
                    player = x;
                }
            }
        }
    }
    return player;
}
Put those in your script, then you can do something like....

pawn Код:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    if(newkeys & KEY_FIRE)
    {
        if(GetPlayerWeapon(playerid) == 4)
        {
            new victimid = GetClosestPlayer(playerid);
            if(IsPlayerConnected(victimid))
            {
                if(GetDistanceBetweenPlayers(playerid,victimid) < 2)
                {
                    new Float:health;
                    GetPlayerHealth(victimid, health);
                    SetPlayerHealth(victimid, health - 50.0);
                    return 1;
                }
            }
        }
    }
    return 1;
}
That'll do the trick. Excuse my indentations.
Reply
#5

I know someone already posted but i made an example so i may aswell post it, i'd do it simalar to this
pawn Код:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    if(PRESSED(KEY_FIRE))
    {
        if(GetPlayerWeapon(i) == 0)
        {
            new Float:x, Float:y, Float:z, hp;
            GetPlayerPos(playerid, x, y, z);
            for(new i; i < MAX_PLAYERS; i++)
            {
                if(!IsPlayerInRangeOfPoint(i, 2.0, x,y,z)continue;//if player isnt in range player do nothing
                else
                {
                    GetPlayerHealth(i, hp);
                    SetPlayerHealth(i, hp-5.0);//remove a little hp because they are in range
                }
            }
            return 1;
        }
    }
    return 1;
}
Reply
#6

Solved
Reply
#7

pawn Код:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    if(PRESSED(KEY_FIRE)) {
        if(GetPlayerWeapon(playerid) == 4) {
            new
                Float:X,
                Float:Y,
                Float:Z,
                Float:hp;
            GetPlayerPos(playerid, X, Y, Z);
            foreach(Player, i)
            {
                if(GetPVarInt(playerid, "teamID") == GetPVarInt(i, "teamID")) return 1; //Change to detect your team options.
                else
                {
                    if(IsPlayerInRangeOfPoint(playerid, 2.0, X, Y, Z))
                    {

                        GetPlayerHealth(i, hp);
                        SetPlayerHealth(i, hp-5.0);
                    }
                }
            }
        }
    }
    return 1;
}

I would recommend using foreach for doing loops like this.
Also, they may be more than 1 mafia in his range so he could punch more than 1 at once and a mafia can't hit I mafia I guess?
Reply
#8

Solved
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)