05.05.2014, 20:18
I guess GetVehicleDistanceFromPoint returns 0 if a vehicle doesn't exist so check if it does.
pawn Code:
GetClosestVehicle(playerid, Float: range)
{
new
vehicleid = INVALID_VEHICLE_ID,
Float: pX,
Float: pY,
Float: pZ,
Float: distance = 9999.0,
Float: tmp_distance;
GetPlayerPos(playerid, pX, pY, pZ);
for (new v = 1; v != MAX_VEHICLES; ++v)
{
if (!GetVehicleModel(v)) continue;
tmp_distance = GetVehicleDistanceFromPoint(v, pX, pY, pZ);
if (tmp_distance <= range && tmp_distance < distance)
{
distance = tmp_distance;
vehicleid = v;
}
}
return vehicleid;
}