SA-MP Forums Archive
[HELP]Making a NPC follow a Player! - 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)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: [HELP]Making a NPC follow a Player! (/showthread.php?tid=139243)



[HELP]Making a NPC follow a Player! - Epic Shower - 04.04.2010

Hello.
I was thinking about, making some NPC's that follow players once they spot them.
But, i don't know how to and i would like someone to show me the codes required to make this happen.
So far, i think that i will need a GetPlayerPos and to put a public OnPlayerStreamIn(playerid) in the NPC Script.
Please don't post links to others filterscripts, that can do this or to other websites.
I only want codes here, so i can understand how it fully works and so later (once i learn it)i can make a [TuT] showing how to and give credits to the person(s) that helped me.
Thanks.



Re: [HELP]Making a NPC follow a Player! - deather - 04.04.2010

Try a timer for getting the player's pos.
Then set the NPC's pos according to the distance.

Like this:

Код:
new timer; // At the top

timer = SetTimerEx("NPCFollow", 100, true, "d", playerid); // where you want to start the NPC to follow.

forward NPCFollow(playerid);
public NPCFollow(playerid)
{
   new Float:x, Float:y, Float:z, pName[50];
   if(!IsPlayerNPC(playerid))
   {
     GetPlayerPos(playerid, x, y, z);
   }
   for(new i=0; i<MAX_PLAYERS; i++)
   {
     if(IsPlayerConnected(i) && IsPlayerNPC(i))
     {
        if(GetDistanceBetweenPlayers(playerid, i)<15)
        {
          SetPlayerPos(i, x-2, y, z);
        }
     }
   }
   return 1;
}