SA-MP Forums Archive
if(IsPlayerInAnyVehicle(playerid)) - 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: if(IsPlayerInAnyVehicle(playerid)) (/showthread.php?tid=660207)



if(IsPlayerInAnyVehicle(playerid)) - Zeus666 - 27.10.2018

How can I detect, if player is in a car, and if it's, how can I /gethere him with the car and passagers still in IT?


PHP код:
CMD:gethere(playerid,params[])
{
    if(
pInfo[playerid][pAdminLevel] >= 1)
    {
        new 
targetid,string[256], str1[256];
          if(
sscanf(params"u"targetid)) SendClientMessage(playerid,-1,""COL_RED"AdmCmds: /gethere [PlayerID]");
        if(!
IsPlayerConnected(targetid)) return SendClientMessage(playerid,-1,""COL_RED"ERROR: Not online!");
        new 
Float:xFloat:yFloat:z;
        
GetPlayerPos(playeridxyz);
        
SetPlayerPos_Allow(targetidx+1y+1z+1);
        
GetPlayerPos(playerid,Float:x,Float:y,Float:z);
        
SetPlayerVirtualWorld(targetid,GetPlayerVirtualWorld(playerid));
        
format(stringsizeof(string), ""COL_RED"AdmCmds: %s %s has brought you to himself."GetAdminName(playerid), PlayerName(playerid));
        
format(str1sizeof(str1), ""COL_RED"%s %s used /gethere upon %s."GetAdminName(playerid),PlayerName(playerid),PlayerName(targetid));
        
SendClientMessage(targetid,-1,string);
        
SendMessageToAllAdmins(str1,-1);
        if(
IsPlayerInAnyVehicle(targetid))
          {
              
SetVehiclePos(GetPlayerVehicleID(targetid),x,y,z);
        }
    }
    else {
        
SendClientMessage(playerid,-1,""COL_RED"EROARE: You are not admin!");
    }
    return 
1;




Re: if(IsPlayerInAnyVehicle(playerid)) - RogueDrifter - 27.10.2018

pawn Код:
SetPlayerOrVehiclePos(playerid, Float:x, Float:y, Float:z) //A function to teleport a player checking if he's on foot or in a vehicle
{
    if(IsPlayerInAnyVehicle(playerid)) //If player is in a vehicle
    {
        SetVehiclePos(GetPlayerVehicleID(playerid), x, y, z); //Teleport vehicle(will also teleport the player alongside)
    }
    else //If he isn't in a vehicle
    {
        SetPlayerPos(playerid, x, y, z); //Teleport him alone
    }
    return true;
}



Re: if(IsPlayerInAnyVehicle(playerid)) - ReD_HunTeR - 27.10.2018

I told you earlier dont create multiple variable, one is enough to send multiple messages...
anyway here you go, with code optimization:
pawn Код:
CMD:gethere(playerid, params[])
{
    if(pInfo[playerid][pAdminLevel] < 1) return SendClientMessage(playerid,-1,""COL_RED"EROARE: You are not admin!");
    new targetid;
    if(sscanf(params, "u", targetid)) SendClientMessage(playerid,-1,""COL_RED"AdmCmds: /gethere [PlayerID]");
    if(!IsPlayerConnected(targetid)) return SendClientMessage(playerid,-1,""COL_RED"ERROR: Not online!");
   
    new Float:x,Float:y,Float:z, string[128];
    SetPlayerInterior(targetid, GetPlayerInterior(playerid));
    SetPlayerVirtualWorld(targetid, GetPlayerVirtualWorld(playerid));
    GetPlayerPos(playerid,Float:x,Float:y,Float:z);
    SetPlayerPos_Allow(targetid,Float:x,Float:y,Float:z);
   
    if(IsPlayerInAnyVehicle(targetid))
    {
        SetVehiclePos(GetPlayerVehicleID(targetid), x, y, z);
        LinkVehicleToInterior(GetPlayerVehicleID(targetid),GetPlayerInterior(playerid));
        PutPlayerInVehicle(targetid, GetPlayerVehicleID(targetid), 0);
    }
   
    format(string, sizeof(string), ""COL_RED"AdmCmds: %s %s has brought you to himself.", GetAdminName(playerid), PlayerName(playerid));
    SendClientMessage(targetid, -1, string);

    format(string, sizeof(string), ""COL_RED"%s %s used /gethere upon %s.", GetAdminName(playerid), PlayerName(playerid), PlayerName(targetid));
    SendMessageToAllAdmins(string, -1);
    return 1;
}
EDIT: didnt see Rogues message, i guess his way is better


Re: if(IsPlayerInAnyVehicle(playerid)) - RogueDrifter - 27.10.2018

Quote:
Originally Posted by ReD_HunTeR
Посмотреть сообщение
I told you earlier dont create multiple variable, one is enough to send multiple messages...
anyway here you go, with code optimization:
pawn Код:
CMD:gethere(playerid, params[])
{
    if(pInfo[playerid][pAdminLevel] < 1) return SendClientMessage(playerid,-1,""COL_RED"EROARE: You are not admin!");
    new targetid;
    if(sscanf(params, "u", targetid)) SendClientMessage(playerid,-1,""COL_RED"AdmCmds: /gethere [PlayerID]");
    if(!IsPlayerConnected(targetid)) return SendClientMessage(playerid,-1,""COL_RED"ERROR: Not online!");
   
    new Float:x,Float:y,Float:z, string[128];
    SetPlayerInterior(targetid, GetPlayerInterior(playerid));
    SetPlayerVirtualWorld(targetid, GetPlayerVirtualWorld(playerid));
    GetPlayerPos(playerid,Float:x,Float:y,Float:z);
    SetPlayerPos_Allow(targetid,Float:x,Float:y,Float:z);
   
    if(IsPlayerInAnyVehicle(targetid))
    {
        SetVehiclePos(GetPlayerVehicleID(targetid), x, y, z);
        LinkVehicleToInterior(GetPlayerVehicleID(targetid),GetPlayerInterior(playerid));
        PutPlayerInVehicle(targetid, GetPlayerVehicleID(targetid), 0);
    }
   
    format(string, sizeof(string), ""COL_RED"AdmCmds: %s %s has brought you to himself.", GetAdminName(playerid), PlayerName(playerid));
    SendClientMessage(targetid, -1, string);

    format(string, sizeof(string), ""COL_RED"%s %s used /gethere upon %s.", GetAdminName(playerid), PlayerName(playerid), PlayerName(targetid));
    SendMessageToAllAdmins(string, -1);
    return 1;
}
EDIT: didnt see Rogues message, i guess his way is better
Why are you using the Float function getting a player's position? And you never checked/set for the vehicle virtual world.

So if you're trying to do it right it with optimization if you're using a function to get a player's feature more than once, it would look more like so:
pawn Код:
CMD:gethere(playerid, params[])
{
    if(pInfo[playerid][pAdminLevel] < 1) return SendClientMessage(playerid,-1,""COL_RED"EROARE: You are not admin!");
    new targetid;
    if(sscanf(params, "u", targetid)) return SendClientMessage(playerid, COLOR_GREY, "USAGE: /get [playerid]");
    if(!IsPlayerConnected(targetid)) return SendClientMessage(playerid,COLOR_RED,"Player is not online");
   
    new Float:x, Float:y, Float:z, string[128], SecondPlayerVeh, FirstPlayerWorld, FirstPlayerInt, bool:VehicleCheck;
    if(IsPlayerInAnyVehicle(targetid)) VehicleCheck = true;

    FirstPlayerInt = GetPlayerInterior(playerid);
    FirstPlayerWorld = GetPlayerVirtualWorld(playerid);
    SecondPlayerVeh = GetPlayerVehicleID(targetid);

    GetPlayerPos(playerid, x, y, z);
    SetPlayerInterior(targetid, FirstPlayerInt);
    SetPlayerVirtualWorld(targetid, FirstPlayerWorld);

    if(VehicleCheck)
    {
        SetVehicleVirtualWorld(SecondPlayerVeh, FirstPlayerWorld);
        SetVehiclePos(SecondPlayerVeh, x, y, z);
        LinkVehicleToInterior(SecondPlayerVeh, FirstPlayerInt);
        PutPlayerInVehicle(targetid, SecondPlayerVeh, 0);
    }
    else SetPlayerPos_Allow(targetid, x, y, z);

    format(string, sizeof(string), ""COL_RED"AdmCmds: %s %s has brought you to himself.", GetAdminName(playerid), PlayerName(playerid));
    SendClientMessage(targetid, -1, string);

    format(string, sizeof(string), ""COL_RED"%s %s used /gethere upon %s.", GetAdminName(playerid), PlayerName(playerid), PlayerName(targetid));
    SendMessageToAllAdmins(string, -1);
    return true;
}
The purpose of the vehiclecheck is just for safety reasons if the player gets ejected out of the vehicle for changing his virtual world and hence not matching with the vehicle's.