GetClosestPlayer returns always 0 ? - 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: GetClosestPlayer returns always 0 ? (
/showthread.php?tid=67314)
GetClosestPlayer returns always 0 ? -
Vince - 28.02.2009
Hi
I have this piece of code:
pawn Код:
dcmd_rob(playerid, params[])
{
#pragma unused params
if(PlayerInfo[playerid][pTeam] == TEAM_COP) return SendClientMessage(playerid, COLOR_RED, "** Police officers cannot rob other people!");
new otherplayer = GetClosestPlayer(playerid);
if(otherplayer == playerid) return SendClientMessage(playerid, COLOR_RED, "** No one close enough to rob!");
SetPlayerCriminal(playerid, 2);
new attempt = random(2);
if(attempt == 1)
{
// If success, give money etc.
}
else
{
format(string, sizeof(string), "You attempted to rob some money from %s, but failed!", PlayerName(otherplayer));
SendClientMessage(playerid, COLOR_YELLOW, string);
}
return 1;
}
GetClosestPlayer always seems to return 0 (when I tested I was playerid 0), even when I stand right next to another person.
Does anyone have a working GetClosestPlayer?
I copied my
GetClosestPlayer function and the other functions it calls from the
uf.inc into a new inc, because I can't include the whole
uf.inc without getting a bunch of errors.
Re: GetClosestPlayer returns always 0 ? -
Nubotron - 28.02.2009
pawn Код:
stock GetClosestPlayer(playerid) //By Slick (edited by Gabriel "Larcius" Cordes)
{
if(IsPlayerConnected(playerid) && IsPlayerConnected(0))
{
new closestplayer=0;
new Float:closestdist=GetDistanceBetweenPlayers(playerid,0);
for(new playerid2=0; playerid2<MAX_PLAYERS; playerid2++)
{
new Float:dist = GetDistanceBetweenPlayers(playerid,playerid2);
if ((dist < closestdist))
{
closestdist = dist;
closestplayer = playerid;
}
}
return closestplayer;
}
return -1;
}
Lol at the guy who wrote/edited this function.
Re: GetClosestPlayer returns always 0 ? -
1337pr0 - 01.03.2009
Replace your current GetClosestPlayer function with this:
pawn Код:
stock GetClosestPlayer(playerid)
{
new Float:cdist, targetid = -1;
for (new i = 0; i < MAX_SLOTS; i++)
{
if (IsPlayerConnected(i) && playerid != i && (targetid < 0 || cdist > GetDistanceBetweenPlayers(playerid, i)))
{
targetid = i;
cdist = GetDistanceBetweenPlayers(playerid, i);
}
}
return targetid;
}