SA-MP Forums Archive
Set player driver in closest vehicle - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Set player driver in closest vehicle (/showthread.php?tid=585774)



Set player driver in closest vehicle - Cenzy - 16.08.2015

Hi, i try for hours to make a command "/command" where the player is set driver of the closest vehicle but is not working...

And (a side question) is any posibility to play a song inside the SA-MP Server from an external source? Without the audio plugin
Can somebody help me? Sorry if i posted in the wrong topic, i'm a newbie in scripting Thank you for your time.


Re: Set player driver in closest vehicle - X337 - 16.08.2015

Use this function:
Код:
stock GetClosestVehicle(playerid)
{
    new Float:p_X,
		Float:p_Y,
		Float:p_Z,
		Float:Distance,
		Float:PretendentDistance = 1000.0,
		Pretendent = INVALID_VEHICLE_ID
	;
    GetPlayerPos(playerid, p_X, p_Y, p_Z);

    foreach(new i : Vehicles)
    {
        Distance = GetVehicleDistanceFromPoint(vehicleid, p_X, p_Y, p_Z);
        if(Distance <= PretendentDistance)
        {
            Pretendent = vehicleid;
            PretendentDistance = Distance;
        }
    }
    return Pretendent;
}
To use this on commands:
Код:
CMD:command(playerid, params[])
{
	new i = GetClosestVehicle(playerid);
	if(i != INVALID_VEHICLE_ID)
	{
		PutPlayerInVehicle(playerid, i, 0);
	}
	return 1;
}
And for sound, take a look at this : https://sampwiki.blast.hk/wiki/PlayAudioStreamForPlayer


Re: Set player driver in closest vehicle - Abagail - 16.08.2015

Quote:
Originally Posted by X337
Посмотреть сообщение
Use this function:
Код:
stock GetClosestVehicle(playerid)
{
    new Float:p_X,
		Float:p_Y,
		Float:p_Z,
		Float:Distance,
		Float:PretendentDistance = 1000.0,
		Pretendent = INVALID_VEHICLE_ID
	;
    GetPlayerPos(playerid, p_X, p_Y, p_Z);

    foreach(new i : Vehicles)
    {
        Distance = GetVehicleDistanceFromPoint(vehicleid, p_X, p_Y, p_Z);
        if(Distance <= PretendentDistance)
        {
            Pretendent = vehicleid;
            PretendentDistance = Distance;
        }
    }
    return Pretendent;
}
To use this on commands:
Код:
CMD:command(playerid, params[])
{
	new i = GetClosestVehicle(playerid);
	if(i != INVALID_VEHICLE_ID)
	{
		PutPlayerInVehicle(playerid, i, 0);
	}
	return 1;
}
And for sound, take a look at this : https://sampwiki.blast.hk/wiki/PlayAudioStreamForPlayer
This actually will sometimes work but will fail hard when someone is already driving the vehicle. You can loop through all connected players & check if their player state is PLAYER_STATE_DRIVER and their vehicle ID matches the Pretent variable.