Sign in to follow this  
DampflokTV

getNearestPlayer + getNearestVehicle

Recommended Posts

Anbei ein Script um den Spieler zu finden, der in einem bestimmten Umkreis am nächsten zu mir steht:

Serverseitig

var currentTarget = null;
function getNearestPlayer(player, range)
{
    let dist = range;
    mp.players.forEachInRange(player.position, range,
        (_player) => {
            if(player != _player)
            {
                let _dist = _player.dist(player.position);
                if(_dist < dist)
                {
                    currentTarget = _player;
                    dist = _dist;
                }
            }
        }
    );
}

Benutzen kann man das dann so:

mp.events.add("event_to_call", (player) => {
    getNearestPlayer(player, 5);
    if(currentTarget)
	{
		// Habe einen oder mehrere Spieler im Umkreis von 5 Metern gefunden und currentTarget ist nun der der am nächsten dran ist.
	}
});

 

Das ganze geht auch mit Fahrzeugen:

var currentTarget = null;

function getNearestVehicle(player, range)
{
    let dist = range;
    mp.vehicles.forEachInRange(player.position, range,
        (_vehicle) => {
            let _dist = _vehicle.dist(player.position);
            if(_dist < dist)
            {
                currentTarget = _vehicle;
                dist = _dist;
            }
        }
    );
}
                            
mp.events.add("event_to_call", (player) => {
    getNearestVehicle(player, 2);
    if(currentTarget)
    {
		// ... siehe oben, prüfen wem Fahrzeug gehört, ob abgeschlossen etc. etc.
    }
});
                            

 

  • Like 4

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.