function Get nearest player?

Started by Kewun, Aug 18, 2016, 08:04 AM

Previous topic - Next topic

Kewun


Kewun


KAKAN

Quote from: Kewun on Aug 18, 2016, 05:24 PMnot worked ;c
show me the function you used. And check for duplicates.
oh no

Kewun

function GetClosestPlayer( source, max_distance = 100 ) {
    local int_MaxPlayers = GetMaxPlayers(), tbl_CloestPlayer = { Instance = null, Distance = max_distance };
        for( local i = 0; i < int_MaxPlayers; ++i ) {
            local player = FindPlayer( i );
            if ( player.ID != source.ID ) {
                local distance = source.Pos.Distance( player.Pos );
                if( distance < tbl_CloestPlayer.Distance ) {
                    tbl_CloestPlayer.Instance = player;
                    tbl_CloestPlayer.Distance = distance;
                }
            }
        }
    return tbl_CloestPlayer.Instance;
}

and

function onArrestBind(player)
{
local plr = GetClosestPlayer(player, 40)
if ( !ReadIniBool("stats.ini","logged",player.Name) ) return false;
if ( player.Skin != 1) return false;
if (!plr) {
MessagePlayer("[#ff0000]Target does not exist",player)
return false;
}
if (plr.WantedLevel <1) {
MessagePlayer("[#ff0000]This player is not wanted",player)
return false;
}
if ( plr.Name == player.Name) {
MessagePlayer("[#ff0000]You cant arrest your self!",player)
return false;
}
if (plr.WantedLevel > 0) {
local distance = player.Pos.Distance(target.Pos)
if ( distance < 5 ) {
if ( plr.Vehicle ) plr.Eject();
MessagePlayer("[#ffffff]Arrested "+plr+" , you get $2500",player)
player.Cash += 2500;
//target.Pos = Vector(386.59, -502.732, 9.39561)
target.Pos = Vector(390.684, -507.166, 9.39561);
target.WantedLevel = 0
NewTimer("BailOut",30000,1,target.Name,target.ID)
MessagePlayer("[#00ff00]You will be bailed out of jail in 30 seconds..",plr)
plr.Skin = 7
plr.Disarm();
SaveAccount(plr)
Message("[#ffffff]Police "+player+ " has arrested "+plr)
}
else {
MessagePlayer("[#00ff00]You must be near wanted target to arrest him",player)
return false;
}
}
}

Kewun


Kewun


KAKAN

try this:-
function GetClosestPlayer( source, max_distance = 100 ) {
    local int_MaxPlayers = GetMaxPlayers(), tbl_CloestPlayer = { Instance = null, Distance = max_distance };
        for( local i = 0; i < int_MaxPlayers; ++i ) {
            if( i == source.ID ) continue;
            local player = FindPlayer( i );
                local distance = source.Pos.Distance( player.Pos );
                if( distance < tbl_CloestPlayer.Distance ) {
                    tbl_CloestPlayer.Instance = player;
                    tbl_CloestPlayer.Distance = distance;
                }
        }
    return tbl_CloestPlayer.Instance;
}
And too, tell me the following output:-
print( typeof source.ID );
print( typeof i );
print( typeof player.ID );
They should be integers
oh no

Kewun

AN ERROR HAS OCCURED [the index 'Pos' does not exist]

CALLSTACK
*FUNCTION [GetClosestPlayer()] server.nut line [955]
*FUNCTION [onArrestBind()] server.nut line [895]
*FUNCTION [onKeyDown()] server.nut line [1444]

LOCALS
[player] NULL
[i] 2
[tbl_CloestPlayer] TABLE
[int_MaxPlayers] 50
[max_distance] 40
[source] INSTANCE
[this] TABLE
[player] INSTANCE
[this] TABLE
[key] 12
[player] INSTANCE
[this] TABLE

function on key down:

function onKeyDown(player,key)
{
    if ( key == action )
    {
       onArrestBind(player)
     }
}
line: onArrestBind(player)

on arrest bind:
function onArrestBind(player)
{
local plr = GetClosestPlayer(player, 40)
if ( !ReadIniBool("stats.ini","logged",player.Name) ) return false;
if ( player.Skin != 1) return false;
if (!plr) {
MessagePlayer("[#ff0000]Target does not exist",player)
return false;
}
if (plr.WantedLevel <1) {
MessagePlayer("[#ff0000]This player is not wanted",player)
return false;
}
if ( plr.Name == player.Name) {
MessagePlayer("[#ff0000]You cant arrest your self!",player)
return false;
}
if (plr.WantedLevel > 0) {
local distance = player.Pos.Distance(target.Pos)
if ( distance < 5 ) {
if ( plr.Vehicle ) plr.Eject();
MessagePlayer("[#ffffff]Arrested "+plr+" , you get $2500",player)
player.Cash += 2500;
//target.Pos = Vector(386.59, -502.732, 9.39561)
target.Pos = Vector(390.684, -507.166, 9.39561);
target.WantedLevel = 0
NewTimer("BailOut",30000,1,target.Name,target.ID)
MessagePlayer("[#00ff00]You will be bailed out of jail in 30 seconds..",plr)
plr.Skin = 7
plr.Disarm();
SaveAccount(plr)
Message("[#ffffff]Police "+player+ " has arrested "+plr)
}
else {
MessagePlayer("[#00ff00]You must be near wanted target to arrest him",player)
return false;
}
}
}
line: local plr = GetClosestPlayer(player, 40)
function GetClosestPlayer( source, max_distance = 100 ) {
    local int_MaxPlayers = GetMaxPlayers(), tbl_CloestPlayer = { Instance = null, Distance = max_distance };
        for( local i = 0; i < int_MaxPlayers; ++i ) {
            if( i == source.ID ) continue;
            local player = FindPlayer( i );
                local distance = source.Pos.Distance( player.Pos );
                if( distance < tbl_CloestPlayer.Distance ) {
                    tbl_CloestPlayer.Instance = player;
                    tbl_CloestPlayer.Distance = distance;
                }
        }
    return tbl_CloestPlayer.Instance;
}
line: local distance = source.Pos.Distance( player.Pos );

DizzasTeR

I'm still wondering why is this all being such a mess, the only mistake in my function was not adding an exception for the function caller, otherwise it just works perfectly. Final function:
function GetClosestPlayer( source, max_distance = 100 ) {
    local int_MaxPlayers = GetMaxPlayers(), tbl_CloestPlayer = { Instance = null, Distance = max_distance };
        for( local i = 0; i < int_MaxPlayers; ++i ) {
            local player = FindPlayer( i );
            if( player && player.ID != source.ID ) {
                local distance = source.Pos.Distance( player.Pos );
                if( distance < tbl_CloestPlayer.Distance ) {
                    tbl_CloestPlayer.Instance = player;
                    tbl_CloestPlayer.Distance = distance;
                }
            }
        }
    return tbl_CloestPlayer.Instance;
}

Tested and works.

Kewun


KAKAN

SLC said the same thing and Kewun said it didn't work :\
oh no

Kewun

if it was same thing, then it would work for me