[Tutorial] [TUT] How to make a car enterable for a certain name only?
#1

Hi,

Since I'm bored, I thought I'd make a little tutorial about how you can make a car enterable for one person only.

First off, we put a variable on top of your script:

pawn Код:
new Matthias; // The name is not really important, this can be anything you want.
Secondly, we place the car in the server, and use Matthias = AddStaticVehicle(...), so the server knows this is the car only I can enter.
If you don't know how to use AddStaticVehicle, check this. This needs to be put in OnGameModeInit or OnFilterScriptInit, depending on what you're using.

pawn Код:
Matthias = AddStaticVehicle(451,1890.1632,1989.1708,13.4920,179.9223,6,6); // This will spawn a turismo in the parking lot in LV
Now this is where the hard part begins, we use OnPlayerStateChange, to check if the player enters a car.

pawn Код:
public OnPlayerStateChange(playerid, newstate, oldstate) // The OnPlayerStateChange function
{
  new PlayerName[24]; // We create a variable which will contain the players name.
  GetPlayerName(playerid, PlayerName, sizeof(PlayerName)); // We use GetPlayerName to store the name in 'PlayerName'.

  if(newstate == PLAYER_STATE_DRIVER) // Check the new state, since he's the driver in this case, we continue.
  {
   new Vehicle = GetPlayerVehicleID(playerid); // This saves the player's vehicle id into the variable 'vehicle'
   if(Vehicle == Matthias) // We check if the vehicle is the one we don't want everyone to enter
   { // It is, so we continue.
     if(strcmp(PlayerName,"Matthias",true)) // We use strcmp to check if the player who entered the car is the same as 'Matthias'
     {
      RemovePlayerFromVehicle(playerid); // It's not, so we remove the player from the car.
      SendClientMessage(playerid, 0x33AA33AA, "I'm sorry, but this car has been reserved for Matthias"); // And we inform him about why he got removed from the car.
     }
   }
  }
  return 1;
}
It's simple as that! If you have done everything as described above, you will have a car in your server which only a player with the name 'Matthias' can enter. Of course you will have to edit this a bit so it fit's nice into your gamemode.
The entire code:

pawn Код:
// On top of your script:

new Matthias;

// In OnGameModeInit or OnFilterScriptInit

Matthias = AddStaticVehicle(451,1890.1632,1989.1708,13.4920,179.9223,6,6);

// Outside other functions

public OnPlayerStateChange(playerid, newstate, oldstate)
{
  new PlayerName[24];
  GetPlayerName(playerid, PlayerName, sizeof(PlayerName));

  if(newstate == PLAYER_STATE_DRIVER)
  {
   new Vehicle = GetPlayerVehicleID(playerid);
   if(Vehicle == Matthias)
   {
     if(strcmp(PlayerName,"Matthias",true))
     {
      RemovePlayerFromVehicle(playerid);
      SendClientMessage(playerid, 0x33AA33AA, "I'm sorry, but this car has been reserved for Matthias");
     }
   }
  }
  return 1;
}
I hope this helped you,
Matthias
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)