In lu there is GetClosestPlayer() but in vcmp no? i want to add a bind key to my server if player presses X, tries to arrest the nearest wanted player
how? i need getclosestplayer function
Use a loop and check each player's distance.
for ex:-
local cplr = 0;
for( local i =0; i< 50; i++ )
{
local plr = FindPlayer( i );
if( !plr ) continue;
local dis = DistanceFromPoint( player.Pos.x, player.Pos.y, plr.Pos.x, plr.Pos.y ); //Correct it, it might be wrong, or use player.Pos.Distance( plr.Pos ) if you want to add z.
if( dis < cplr ) cplr = dis;
}
player.Msg( cplr );
Correct it, it might be wrong. Just an example.
hmhmhmmrhmhmhphm... okay ill test this thing out and mess with it.. ill mark this topic as solved if i will make it work..
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 ) {
local distance = source.Pos.Distance( player.Pos );
if( distance < tbl_CloestPlayer.Distance ) {
tbl_CloestPlayer.Instance = player;
tbl_CloestPlayer.Distance = distance;
}
}
}
return tbl_CloestPlayer.Instance;
}
// ----
local plr = GetClosestPlayer( player, 40 ); // return the closest player in range of 40m
Untested, if you want to get nearest player, specially inside cases that it calls the function everytime 'X' is pressed, then you should keep a max range, you don't want people in one corner of the map to arrest a guy in the opposite corner if that's the nearest to him.
if i tested, GetClosestPlayer returns myself ;c :( even if there are plrs on serv. in your script doomkiller
Quote from: Kewun on Aug 18, 2016, 01:02 PMif i tested, GetClosestPlayer returns myself ;c :( even if there are plrs on serv. in your script doomkiller
That sure must be hard to fix yourself..
not hard, im trying to fix it with my self
Did you test it with multiple people on the server? I won't be surprised to hear a negative answer.
Quote from: Doom_Kill3R on Aug 18, 2016, 01:11 PMDid you test it with multiple people on the server? I won't be surprised to hear a negative answer.
It's actually what was supposed to happen because the source player is the closest to itself and there's no guard against that.
The following code:
if( player ) {
Could be changed into:
if( player != null && player != source ) {
Which can probably be collapsed into:
if( player != source ) {
Assuming that `source` is not null.
Quote from: Doom_Kill3R on Aug 18, 2016, 01:11 PMDid you test it with multiple people on the server? I won't be surprised to hear a negative answer.
tested with multiple players.
even if ( player != source ) { didnt work
ill try to fix..
well after using if ( player != source ) { i could'nt arrest anyone
Quote from: Kewun on Aug 18, 2016, 01:22 PMwell after using if ( player != source ) { i could'nt arrest anyone
show us your updated code.
it should be:-
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 != null && player != source ) {
local distance = source.Pos.Distance( player.Pos );
if( distance < tbl_CloestPlayer.Distance ) {
tbl_CloestPlayer.Instance = player;
tbl_CloestPlayer.Distance = distance;
}
}
}
return tbl_CloestPlayer.Instance;
}
// ----
local plr = GetClosestPlayer( player, 40 ); // return the closest player in range of 40m
i tried it. Shows myself.
damn shit fixing it is hard for me a bit
Use original code and compare their IDs. Comparing instances will not get you anywhere.
so i will have to use if ( player.ID != source.ID ) {
?
not worked ;c
Quote from: Kewun on Aug 18, 2016, 05:24 PMnot worked ;c
show me the function you used. And check for duplicates.
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;
}
}
}
i also tried if ( source.ID != player.ID ) {
:/ i tried to fix, i failed
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
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 );
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.
thx bro it works <3
SLC said the same thing and Kewun said it didn't work :\
if it was same thing, then it would work for me