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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.