Specific vehicle
#1

Hello

I am trying to do something like only admins can get into vehicle id 432 and if players go in get slapped.I did this and its not working
pawn Код:
public OnPlayerEnterVehicle(playerid, vehicleid) {
    for(new x=0; x<MAX_PLAYERS; x++) {
        if(GetPlayerState(x) == PLAYER_STATE_SPECTATING && PlayerInfo[x][SpecID] == playerid) {
            TogglePlayerSpectating(x, 1);
            PlayerSpectateVehicle(x, vehicleid);
            PlayerInfo[x][SpecType] = ADMIN_SPEC_TYPE_VEHICLE;
        }
    }
   
     if(PlayerInfo[playerid][Level] >= 0)
     {
    if(vehicleid == ANTIVEH)
    {
        SendClientMessage(playerid, COLOR_GREEN, "balnh.");
        new Float:Health, Float:x, Float:y, Float:z; GetPlayerHealth(playerid,Health); SetPlayerHealth(playerid,Health-25);
        GetPlayerPos(playerid,x,y,z); SetPlayerPos(playerid,x,y,z+5); PlayerPlaySound(playerid,1190,0.0,0.0,0.0); PlayerPlaySound(playerid,1190,0.0,0.0,0.0);
     }
  }
    return 1;
}
Код:
#define ANTIVEH 432
Reply
#2

Rather than defining the vehicle id, use a variable as the vehicleid may change.

pawn Код:
/* Outside OnGameModeInit */
new gAdminVehicle;

/* Inside OnGameModeInit (or where ever the vehicle is created) */
gAdminVehicle = AddStaticVehicle(...);

/* inside OnPlayerEnterVehicle */
// If the players level is not an admin level
if (PlayerInfo[playerid][Level] == 0)
{
    // If the vehiclid the player is entering is our admin vehicle
    if (vehicleid == gAdminVehicle)
    {
        // Do what you want with the player
    }
}
Reply
#3

Nah i dont wanna use a variable as i got too many rhinos in server.

I want it in some kinda the method i used which is nt successful
Reply
#4

TRY THIS

pawn Код:
public OnPlayerEnterVehicle(playerid, vehicleid) {
    for(new x=0; x<MAX_PLAYERS; x++) {
        if(GetPlayerState(x) == PLAYER_STATE_SPECTATING && PlayerInfo[x][SpecID] == playerid) {
            TogglePlayerSpectating(x, 1);
            PlayerSpectateVehicle(x, vehicleid);
            PlayerInfo[x][SpecType] = ADMIN_SPEC_TYPE_VEHICLE;
        }
    }

    if(vehicleid == ANTIVEH)
    {
        if(IsPlayerConnected(playerid))
        {
            if(PlayerInfo[playerid][Level] >= 0)
            {
                SendClientMessage(playerid, COLOR_GREEN, "Welcome Admin in Admin Restricted Vehicle.");
            }
            else
            {
                SendClientMessage(playerid, COLOR_GREEN, "balnh.");
                new Float:Health, Float:x, Float:y, Float:z; GetPlayerHealth(playerid,Health); SetPlayerHealth(playerid,Health-25);
                GetPlayerPos(playerid,x,y,z); SetPlayerPos(playerid,x,y,z+5); PlayerPlaySound(playerid,1190,0.0,0.0,0.0); PlayerPlaySound(playerid,1190,0.0,0.0,0.0);
            }
        }
    }
    return 1;
}
Reply
#5

If you use #define then your script is not future proof. Adding a new vehicle may change the ID of your define there for breaking your code until you change the define. There's nothing wrong with using variables. There's also nothing wrong with storing EVERY vehicles ID, as long as you need it.

If you want to store multiple vehicles without declaring hundreds of variables you can use an array.

pawn Код:
/* Outside OnGameModeInit */
// Change 5 to the number of vehicles you want to store
new gAdminVehicles[5];

/* Inside OnGameModeInit (or where ever the vehicle is created) */
gAdminVehicle[0] = AddStaticVehicle(...);
gAdminVehicle[1] = AddStaticVehicle(...);
gAdminVehicle[2] = AddStaticVehicle(...);
gAdminVehicle[3] = AddStaticVehicle(...);
gAdminVehicle[4] = AddStaticVehicle(...);

/* inside OnPlayerEnterVehicle */
// If the players level is not an admin level
if (PlayerInfo[playerid][Level] == 0)
{
    for (new i; i < sizeof(gAdminVehicles); i++)
    {
        if (vehicleid == gAdminVehicle[i])
        {
            // Do what you want with the player
       
            // break out of the loop as we have found our vehicle
            break;
        }
    }
}
Just a small example. If you need to know more have a look for array tutorials.

@Danyal: Surely if a player player is entering a vehicle he must be connected, right?
Reply
#6

Not working
Reply
#7

pawn Код:
public OnPlayerEnterVehicle(playerid, vehicleid) {
    new vehicle;
    vehicle = GetPlayerVehicleID(playerid);
    for(new x=0; x<MAX_PLAYERS; x++) {
        if(GetPlayerState(x) == PLAYER_STATE_SPECTATING && PlayerInfo[x][SpecID] == playerid) {
            TogglePlayerSpectating(x, 1);
            PlayerSpectateVehicle(x, vehicleid);
            PlayerInfo[x][SpecType] = ADMIN_SPEC_TYPE_VEHICLE;
        }
    }

    if(GetVehicleModel(vehicleid) == ANTIVEH)
    {
        if(IsPlayerConnected(playerid))
        {
            if(PlayerInfo[playerid][Level] > 0)
            {
                SendClientMessage(playerid, COLOR_GREEN, "Welcome Admin in Admin Restricted Vehicle.");
            }
            else
            {
                SendClientMessage(playerid, COLOR_GREEN, "balnh.");
                new Float:Health, Float:x, Float:y, Float:z; GetPlayerHealth(playerid,Health); SetPlayerHealth(playerid,Health-25);
                GetPlayerPos(playerid,x,y,z); SetPlayerPos(playerid,x,y,z+5); PlayerPlaySound(playerid,1190,0.0,0.0,0.0); PlayerPlaySound(playerid,1190,0.0,0.0,0.0);
            }
        }
        return 1;
    }
    return 1;
}
Try Now?


EDITED...
Reply
#8

It worked but it lets anyone to get into vehicle and send message welcome admin in admin i want if player lvl is 0 will get ejected
Reply
#9

Check my last post i edited it...


Hope fully it will work
Reply
#10

Are you talking about the vehicles model or ID? Your first post implies the vehicles ID but if you mean model, try this.

pawn Код:
/* Outside OnGameModeInit */
#define ADMIN_VEHICLE_MODEL            432

/* inside OnPlayerEnterVehicle */
// If the players level is not an admin level
if (PlayerInfo[playerid][Level] == 0)
{
    // If the vehiclid the player is entering is our admin vehicle model
    if (GetVehicleModel(vehicleid) == ADMIN_VEHICLE_MODEL)
    {
        // Do what you want with the player
    }
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)