SA-MP Forums Archive
Check if any player is near NPC in NPC Mode? - 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: Check if any player is near NPC in NPC Mode? (/showthread.php?tid=133042)



Check if any player is near NPC in NPC Mode? - Programie - 10.03.2010

Hi,

How can I check if a player is near a NPC?
I want to stop moving a NPC if the player gets closer to a NPC.
But if a NPC is to close to another NPC, they both stops forever.
IsPlayerNPC is just working in a filter script or the game mode...


Is it possible?


Re: Check if any player is near NPC in NPC Mode? - kingforyou - 11.03.2010

I want to know too =]


Re : Check if any player is near NPC in NPC Mode? - timaoux - 28.09.2011

pawn Код:
public onplayerupdate(playerid)
{
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(IsPlayerConnected(i))
        {
            if(IsPlayerNPC(i)
            {
                new Float:X, Float:Y, Float:Z;
                GetPlayerPos(i, X, Y, Z);
                if(IsPlayerInRangeOfPoint(playerid, 5.0, X, Y, Z))
                {
                    //Your function here when a player is near a npc
                }
            }
        }
    }
    return 1;
}

sorry for report this topic


Re: Re : Check if any player is near NPC in NPC Mode? - SuperViper - 28.09.2011

You should be using foreach and not OnPlayerUpdate.

Include YSI\y_timers and foreach and make a new timer

pawn Код:
Timer:UpdatePlayer[250]() {
    foreach(Player, playerid) {
        if(!IsPlayerNPC(playerid)) {
            foreach(Player, npcid) {
                if(IsPlayerNPC(npcid)) {
                    new Float:rPos[3];
                    GetPlayerPos(npcid, rPos[0], rPos[1], rPos[2]);
                    if(IsPlayerInRangeOfPoint(playerid, 8.0, rPos[0], rPos[1], rPos[2])) {
                        OnPlayerNearNPC(playerid, npcid);
                    }
                }
             }
         }
    }
}
After you do that, create a new function called OnPlayerNearNPC.

pawn Код:
OnPlayerNearNPC(playerid, npcid)
{
    // Code inside here
}
Enjoy. This is faster than the one above me.