SA-MP Forums Archive
OnPlayerTakeDamage [+REP] - 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)
+--- Thread: OnPlayerTakeDamage [+REP] (/showthread.php?tid=574807)



OnPlayerTakeDamage [+REP] - arlindi - 20.05.2015

PHP код:
public OnPlayerTakeDamage(playerid, issuerid, Float: amount, weaponid)
{
    
PlayerShotPlayer[issuerid][playerid] = 1;
    if(
team[issuerid] == TEAM_HUMAN)
    {
        if(
pInfo[issuerid][pHumanClass] == KICKBACK || pInfo[issuerid][pVipKickBack] == 1 || pInfo[issuerid][pKickBackCoin])
        {
            if(
team[playerid] == TEAM_ZOMBIE)
            {
                if(
weaponid == 23 || weaponid == 25 || weaponid == 24 || weaponid == 34 || weaponid == 31 || weaponid == 29)
                {
                    new 
Float:x,Float:y,Float:z,Float:angle;
                    
GetPlayerFacingAngle(playerid,Float:angle);
                    
GetPlayerVelocity(playerid,Float:x,Float:y,Float:z);
                    
SetPlayerVelocity(playerid,Float:x+0.1,Float:y+0.1,Float:z+0.2);
                    
SetPlayerFacingAngle(playerid,Float:angle);
                }
            }
        }
    }
    if(
team[issuerid] == TEAM_HUMAN)
    {
        if(
pInfo[issuerid][pHumanClass] == KICKBACK || pInfo[issuerid][pVipKickBack] == 1 || pInfo[issuerid][pKickBackCoin])
        {
            if(
pInfo[issuerid][pZombieClass] == KZOM)
            {
                if(
weaponid == 23 || weaponid == 25 || weaponid == 24 || weaponid == 34 || weaponid == 31 || weaponid == 29 || weaponid == 27)
                {
                    new 
Float:x,Float:y,Float:z,Float:angle;
                    
GetPlayerFacingAngle(playerid,Float:angle);
                    
GetPlayerVelocity(playerid,Float:x,Float:y,Float:z);
                    
SetPlayerVelocity(playerid,Float:x+0.0,Float:y+0.0,Float:z+0.0);
                    
SetPlayerFacingAngle(playerid,Float:angle);
                }
            }
        }
    }
    if(
team[issuerid] == TEAM_ZOMBIE)
    {
        if(
pInfo[issuerid][pZombieClass] == DSNIPE)
        {
            if(
team[playerid] == TEAM_HUMAN)
            {
                if(
weaponid == 34)
                {
                new 
Float:hp;
                
GetPlayerHealth(playerid,hp);
                
SetPlayerHealth(playerid, hp - 10);
                }
            }
        }
    }
    if(
team[issuerid] == TEAM_HUMAN)
    {
        if(
pInfo[issuerid][pHumanClass] == AKICK)
        {
            if(
team[playerid] == TEAM_ZOMBIE)
            {
                if(
weaponid == 27)
                {
                    new 
Float:x,Float:y,Float:z,Float:angle;
                    
GetPlayerFacingAngle(playerid,Float:angle);
                    
GetPlayerVelocity(playerid,Float:x,Float:y,Float:z);
                    
SetPlayerVelocity(playerid,Float:x+0.3,Float:y+0.3,Float:z+0.4);
                    
SetPlayerFacingAngle(playerid,Float:angle);
                }
            }
        }
    }
    if(
team[issuerid] == TEAM_HUMAN)
    {
        if(
pInfo[issuerid][pHumanClass] == VIPSCOUT || pInfo[issuerid][pHumanClass] == MASTER || pInfo[issuerid][pHumanClass] == BUDDY || pInfo[issuerid][pHumanClass] == HITMAN)
        {
            if(
team[playerid] == TEAM_ZOMBIE)
            {
                if(
weaponid == 34 || weaponid == 29 || weaponid == 14 || weaponid == 33 || weaponid == 30)
                {
                new 
Float:hp;
                
GetPlayerHealth(playerid,hp);
                
SetPlayerHealth(playerid, hp - 40);
                }
            }
        }
    }
    if(
team[issuerid] == TEAM_ZOMBIE)
    {
        if(
pInfo[issuerid][pZombieClass] == GZOM)
        {
            if(
team[playerid] == TEAM_HUMAN)
            {
                if(
weaponid == 9)
                {
                new 
Float:hp;
                
GetPlayerHealth(playerid,hp);
                
SetPlayerHealth(playerid, hp - 40);
                }
            }
        }
    }
    if(
team[issuerid] == TEAM_ZOMBIE || TEAM_HUMAN)
    {
        if(
pInfo[issuerid][pHour] <= 2)
        {
            if(
team[playerid] == TEAM_HUMAN)
            {
                
pInfo[issuerid][pXP] += 10;
            }
        }
    }
    if(
team[issuerid] == TEAM_HUMAN)
    {
        if(
pInfo[issuerid][pHumanClass] == COWBOY)
        {
            if(
team[playerid] == TEAM_ZOMBIE)
            {
                if(
weaponid == 22)
                {
                new 
Float:hp;
                
GetPlayerHealth(playerid,hp);
                
SetPlayerHealth(playerid, hp - 20);
                }
            }
        }
    }
    if(
team[issuerid] == TEAM_HUMAN)
    {
        if(
pInfo[issuerid][pHumanClass] == ASSASSIN)
        {
            if(
team[playerid] == TEAM_ZOMBIE)
            {
                if(
weaponid == 8)
                {
                new 
Float:hp;
                
GetPlayerHealth(playerid,hp);
                
SetPlayerHealth(playerid, hp - 100);
                }
            }
        }
    }
    if(
team[issuerid] == TEAM_HUMAN)
    {
        if(
pInfo[issuerid][pHumanClass] == PIMP)
        {
            if(
team[playerid] == TEAM_ZOMBIE)
            {
                if(
weaponid == 10)
                {
                new 
Float:hp;
                
GetPlayerHealth(playerid,hp);
                
SetPlayerHealth(playerid, hp - 15);
                }
            }
        }
    }
    if(
team[issuerid] == TEAM_HUMAN)
    {
        if(
pInfo[issuerid][pHumanClass] == EXPERIENCEDSCOUT)
        {
            if(
team[playerid] == TEAM_ZOMBIE)
            {
                if(
weaponid == 34 || weaponid == 23 )
                {
                new 
Float:hp;
                
GetPlayerHealth(playerid,hp);
                
SetPlayerHealth(playerid, hp - 80);
                }
            }
        }
    }
    if(
team[issuerid] == TEAM_HUMAN)
    {
        if(
pInfo[issuerid][pHumanClass] == SCOUT || pInfo[issuerid][pHumanClass] == HEAVYSHOTGUN || pInfo[issuerid][pHumanClass] == KICKBACK || pInfo[issuerid][pDamageShotgunCoin] == 1 || pInfo[issuerid][pHumanClass] == BOXER)
        {
            if(
team[playerid] == TEAM_ZOMBIE)
            {
                if(
weaponid == 25)
                {
                new 
Float:hp;
                
GetPlayerHealth(playerid,hp);
                
SetPlayerHealth(playerid, hp - 35);
                }
                if(
weaponid == 0)
                {
                new 
Float:hp;
                
GetPlayerHealth(playerid,hp);
                
SetPlayerHealth(playerid, hp - 100);
                }
                if(
weaponid == 34)
                {
                new 
Float:hp;
                
GetPlayerHealth(playerid,hp);
                
SetPlayerHealth(playerid, hp - 35);
                }
            }
        }
    }
    if(
team[issuerid] == TEAM_HUMAN)
    {
        if(
pInfo[issuerid][pHumanClass] == TECG)
        {
            if(
team[playerid] == TEAM_ZOMBIE)
            {
                if(
weaponid == 32)
                {
                new 
Float:hp;
                
GetPlayerHealth(playerid,hp);
                
SetPlayerHealth(playerid, hp - 35);
                }
            }
        }
    }
    if(
team[issuerid] == TEAM_HUMAN)
    {
        if(
pInfo[issuerid][pHumanClass] == BULL)
        {
            if(
team[playerid] == TEAM_ZOMBIE)
            {
                if(
weaponid == 26)
                {
                new 
Float:hp;
                
GetPlayerHealth(playerid,hp);
                
SetPlayerHealth(playerid, hp - 40);
                }
            }
        }
    }
    if(
team[issuerid] == TEAM_HUMAN)
    {
        if(
pInfo[issuerid][pHumanClass] == ELITE)
        {
            if(
team[playerid] == TEAM_ZOMBIE)
            {
                if(
weaponid == 26)
                {
                new 
Float:hp;
                
GetPlayerHealth(playerid,hp);
                
SetPlayerHealth(playerid, hp - 20);
                }
                if(
weaponid == 24)
                {
                new 
Float:hp;
                
GetPlayerHealth(playerid,hp);
                
SetPlayerHealth(playerid, hp - 70);
                }
                if(
weaponid == 32)
                {
                new 
Float:hp;
                
GetPlayerHealth(playerid,hp);
                
SetPlayerHealth(playerid, hp - 5);
                }
                if(
weaponid == 31)
                {
                new 
Float:hp;
                
GetPlayerHealth(playerid,hp);
                
SetPlayerHealth(playerid, hp - 15);
                }
            }
        }
    }
    if(
team[issuerid] == TEAM_HUMAN)
    {
        if(
pInfo[issuerid][pHumanClass] == HERSHEL)
        {
            if(
team[playerid] == TEAM_ZOMBIE)
            {
                if(
weaponid == 25)
                {
                new 
Float:hp;
                
GetPlayerHealth(playerid,hp);
                
SetPlayerHealth(playerid, hp - 30);
                }
            }
        }
    }
/*    if(issuerid != INVALID_PLAYER_ID) // Spawn KIll
    {
        if(team[issuerid] == TEAM_HUMAN && team[playerid] == TEAM_ZOMBIE)
        {
            if(IsPlayerInRangeOfPoint(playerid,5,Map[ZombieSpawnX],Map[ZombieSpawnY],Map[ZombieSpawnZ]))
            {
                SetPlayerHealth(issuerid, 0);
                new Float:hp;
                new string[256];
                GetPlayerHealth(playerid, hp);
                SetPlayerHealth(playerid, hp+amount);
                GameTextForPlayer(issuerid, "~w~You are zombie because you were~n~spawn killing", 5000,3);
                format(string,sizeof(string), "%s has been set to zombie because he/she was spawn killing!",PlayerName(issuerid));
                SendClientMessageToAll(0x800000C8,string);
            }
        }
    }*/
    
return 1;
} 
OnPlayerTakeDamageFunction


I get this in server log

PHP код:
[14:42:34] [debug] Run time error 4: "Array index out of bounds"
[14:42:34] [debug]  Accessing element at index 65535 past array upper bound 999
[14:42:34] [debug] AMX backtrace:
[
14:42:34] [debug] #0 00038794 in public OnPlayerTakeDamage (0, 65535, 1079194420, 54, 3) from zma4.amx 



Re: OnPlayerTakeDamage [+REP] - Konstantinos - 20.05.2015

Check if issuerid is not INVALID_PLAYER_ID before using it in arrays.


Re: OnPlayerTakeDamage [+REP] - vyper - 20.05.2015

It's because you don't check if issuerid is a valid ID, therefore when you damage yourself by falling for example the issuerid is going to be 65535 instead of an id that's inside the array bounds (specified in this case MAX_PLAYERS aka 1000).


Re: OnPlayerTakeDamage [+REP] - arlindi - 20.05.2015

But i have removed it to /* */


Re: OnPlayerTakeDamage [+REP] - vyper - 20.05.2015

The issuerid validation check must be on top of the callback.


Re: OnPlayerTakeDamage [+REP] - arlindi - 20.05.2015

How to check the issuerid ?


Re: OnPlayerTakeDamage [+REP] - Konstantinos - 20.05.2015

It's exactly the same as the commented part.
PHP код:
public OnPlayerTakeDamage(playerid, issuerid, Float: amount, weaponid) 
{ 
    if (
issuerid != INVALID_PLAYER_ID)
    {
        
// code where "issuerid" is used in arrays..
    
}
    return 
1;
} 



Re: OnPlayerTakeDamage [+REP] - vyper - 20.05.2015

Wrap all your code in this check:

Код:
if(issuerid != INVALID_PLAYER_ID)
{    
      your callback code here..
}



Re: OnPlayerTakeDamage [+REP] - arlindi - 20.05.2015

PHP код:
public OnPlayerTakeDamage(playerid, issuerid, Float: amount, weaponid)
{
    
PlayerShotPlayer[issuerid][playerid] = 1;
  if(
issuerid != INVALID_PLAYER_ID) // Spawn KIll
  
{
    if(
team[issuerid] == TEAM_HUMAN)
    {
        if(
pInfo[issuerid][pHumanClass] == KICKBACK || pInfo[issuerid][pVipKickBack] == 1 || pInfo[issuerid][pKickBackCoin])
        {
            if(
team[playerid] == TEAM_ZOMBIE)
            {
                if(
weaponid == 23 || weaponid == 25 || weaponid == 24 || weaponid == 34 || weaponid == 31 || weaponid == 29)
                {
                    new 
Float:x,Float:y,Float:z,Float:angle;
                    
GetPlayerFacingAngle(playerid,Float:angle);
                    
GetPlayerVelocity(playerid,Float:x,Float:y,Float:z);
                    
SetPlayerVelocity(playerid,Float:x+0.1,Float:y+0.1,Float:z+0.2);
                    
SetPlayerFacingAngle(playerid,Float:angle);
                }
            }
        }
    }
    if(
team[issuerid] == TEAM_HUMAN)
    {
        if(
pInfo[issuerid][pHumanClass] == KICKBACK || pInfo[issuerid][pVipKickBack] == 1 || pInfo[issuerid][pKickBackCoin])
        {
            if(
pInfo[issuerid][pZombieClass] == KZOM)
            {
                if(
weaponid == 23 || weaponid == 25 || weaponid == 24 || weaponid == 34 || weaponid == 31 || weaponid == 29 || weaponid == 27)
                {
                    new 
Float:x,Float:y,Float:z,Float:angle;
                    
GetPlayerFacingAngle(playerid,Float:angle);
                    
GetPlayerVelocity(playerid,Float:x,Float:y,Float:z);
                    
SetPlayerVelocity(playerid,Float:x+0.0,Float:y+0.0,Float:z+0.0);
                    
SetPlayerFacingAngle(playerid,Float:angle);
                }
            }
        }
    }
    if(
team[issuerid] == TEAM_ZOMBIE)
    {
        if(
pInfo[issuerid][pZombieClass] == DSNIPE)
        {
            if(
team[playerid] == TEAM_HUMAN)
            {
                if(
weaponid == 34)
                {
                new 
Float:hp;
                
GetPlayerHealth(playerid,hp);
                
SetPlayerHealth(playerid, hp - 10);
                }
            }
        }
    }
    if(
team[issuerid] == TEAM_HUMAN)
    {
        if(
pInfo[issuerid][pHumanClass] == AKICK)
        {
            if(
team[playerid] == TEAM_ZOMBIE)
            {
                if(
weaponid == 27)
                {
                    new 
Float:x,Float:y,Float:z,Float:angle;
                    
GetPlayerFacingAngle(playerid,Float:angle);
                    
GetPlayerVelocity(playerid,Float:x,Float:y,Float:z);
                    
SetPlayerVelocity(playerid,Float:x+0.3,Float:y+0.3,Float:z+0.4);
                    
SetPlayerFacingAngle(playerid,Float:angle);
                }
            }
        }
    }
    if(
team[issuerid] == TEAM_HUMAN)
    {
        if(
pInfo[issuerid][pHumanClass] == VIPSCOUT || pInfo[issuerid][pHumanClass] == MASTER || pInfo[issuerid][pHumanClass] == BUDDY || pInfo[issuerid][pHumanClass] == HITMAN)
        {
            if(
team[playerid] == TEAM_ZOMBIE)
            {
                if(
weaponid == 34 || weaponid == 29 || weaponid == 14 || weaponid == 33 || weaponid == 30)
                {
                new 
Float:hp;
                
GetPlayerHealth(playerid,hp);
                
SetPlayerHealth(playerid, hp - 40);
                }
            }
        }
    }
    if(
team[issuerid] == TEAM_ZOMBIE)
    {
        if(
pInfo[issuerid][pZombieClass] == GZOM)
        {
            if(
team[playerid] == TEAM_HUMAN)
            {
                if(
weaponid == 9)
                {
                new 
Float:hp;
                
GetPlayerHealth(playerid,hp);
                
SetPlayerHealth(playerid, hp - 40);
                }
            }
        }
    }
    if(
team[issuerid] == TEAM_ZOMBIE || TEAM_HUMAN)
    {
        if(
pInfo[issuerid][pHour] <= 2)
        {
            if(
team[playerid] == TEAM_HUMAN)
            {
                
pInfo[issuerid][pXP] += 10;
            }
        }
    }
    if(
team[issuerid] == TEAM_HUMAN)
    {
        if(
pInfo[issuerid][pHumanClass] == COWBOY)
        {
            if(
team[playerid] == TEAM_ZOMBIE)
            {
                if(
weaponid == 22)
                {
                new 
Float:hp;
                
GetPlayerHealth(playerid,hp);
                
SetPlayerHealth(playerid, hp - 20);
                }
            }
        }
    }
    if(
team[issuerid] == TEAM_HUMAN)
    {
        if(
pInfo[issuerid][pHumanClass] == ASSASSIN)
        {
            if(
team[playerid] == TEAM_ZOMBIE)
            {
                if(
weaponid == 8)
                {
                new 
Float:hp;
                
GetPlayerHealth(playerid,hp);
                
SetPlayerHealth(playerid, hp - 100);
                }
            }
        }
    }
    if(
team[issuerid] == TEAM_HUMAN)
    {
        if(
pInfo[issuerid][pHumanClass] == PIMP)
        {
            if(
team[playerid] == TEAM_ZOMBIE)
            {
                if(
weaponid == 10)
                {
                new 
Float:hp;
                
GetPlayerHealth(playerid,hp);
                
SetPlayerHealth(playerid, hp - 15);
                }
            }
        }
    }
    if(
team[issuerid] == TEAM_HUMAN)
    {
        if(
pInfo[issuerid][pHumanClass] == EXPERIENCEDSCOUT)
        {
            if(
team[playerid] == TEAM_ZOMBIE)
            {
                if(
weaponid == 34 || weaponid == 23 )
                {
                new 
Float:hp;
                
GetPlayerHealth(playerid,hp);
                
SetPlayerHealth(playerid, hp - 80);
                }
            }
        }
    }
    if(
team[issuerid] == TEAM_HUMAN)
    {
        if(
pInfo[issuerid][pHumanClass] == SCOUT || pInfo[issuerid][pHumanClass] == HEAVYSHOTGUN || pInfo[issuerid][pHumanClass] == KICKBACK || pInfo[issuerid][pDamageShotgunCoin] == 1 || pInfo[issuerid][pHumanClass] == BOXER)
        {
            if(
team[playerid] == TEAM_ZOMBIE)
            {
                if(
weaponid == 25)
                {
                new 
Float:hp;
                
GetPlayerHealth(playerid,hp);
                
SetPlayerHealth(playerid, hp - 35);
                }
                if(
weaponid == 0)
                {
                new 
Float:hp;
                
GetPlayerHealth(playerid,hp);
                
SetPlayerHealth(playerid, hp - 100);
                }
                if(
weaponid == 34)
                {
                new 
Float:hp;
                
GetPlayerHealth(playerid,hp);
                
SetPlayerHealth(playerid, hp - 35);
                }
            }
        }
    }
    if(
team[issuerid] == TEAM_HUMAN)
    {
        if(
pInfo[issuerid][pHumanClass] == TECG)
        {
            if(
team[playerid] == TEAM_ZOMBIE)
            {
                if(
weaponid == 32)
                {
                new 
Float:hp;
                
GetPlayerHealth(playerid,hp);
                
SetPlayerHealth(playerid, hp - 35);
                }
            }
        }
    }
    if(
team[issuerid] == TEAM_HUMAN)
    {
        if(
pInfo[issuerid][pHumanClass] == BULL)
        {
            if(
team[playerid] == TEAM_ZOMBIE)
            {
                if(
weaponid == 26)
                {
                new 
Float:hp;
                
GetPlayerHealth(playerid,hp);
                
SetPlayerHealth(playerid, hp - 40);
                }
            }
        }
    }
    if(
team[issuerid] == TEAM_HUMAN)
    {
        if(
pInfo[issuerid][pHumanClass] == ELITE)
        {
            if(
team[playerid] == TEAM_ZOMBIE)
            {
                if(
weaponid == 26)
                {
                new 
Float:hp;
                
GetPlayerHealth(playerid,hp);
                
SetPlayerHealth(playerid, hp - 20);
                }
                if(
weaponid == 24)
                {
                new 
Float:hp;
                
GetPlayerHealth(playerid,hp);
                
SetPlayerHealth(playerid, hp - 70);
                }
                if(
weaponid == 32)
                {
                new 
Float:hp;
                
GetPlayerHealth(playerid,hp);
                
SetPlayerHealth(playerid, hp - 5);
                }
                if(
weaponid == 31)
                {
                new 
Float:hp;
                
GetPlayerHealth(playerid,hp);
                
SetPlayerHealth(playerid, hp - 15);
                }
            }
        }
    }
    if(
team[issuerid] == TEAM_HUMAN)
    {
        if(
pInfo[issuerid][pHumanClass] == HERSHEL)
        {
            if(
team[playerid] == TEAM_ZOMBIE)
            {
                if(
weaponid == 25)
                {
                new 
Float:hp;
                
GetPlayerHealth(playerid,hp);
                
SetPlayerHealth(playerid, hp - 30);
                }
            }
        }
    }
/*
        if(team[issuerid] == TEAM_HUMAN && team[playerid] == TEAM_ZOMBIE)
        {
            if(IsPlayerInRangeOfPoint(playerid,5,Map[ZombieSpawnX],Map[ZombieSpawnY],Map[ZombieSpawnZ]))
            {
                SetPlayerHealth(issuerid, 0);
                new Float:hp;
                new string[256];
                GetPlayerHealth(playerid, hp);
                SetPlayerHealth(playerid, hp+amount);
                GameTextForPlayer(issuerid, "~w~You are zombie because you were~n~spawn killing", 5000,3);
                format(string,sizeof(string), "%s has been set to zombie because he/she was spawn killing!",PlayerName(issuerid));
                SendClientMessageToAll(0x800000C8,string);
            }
        }
    }*/
    
}
    return 
1;
} 
Like this ?


Re: OnPlayerTakeDamage [+REP] - rappy93 - 20.05.2015

Yes, thats what they meant. Give it a try.