[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
#2

Good job! This is really usefull
Reply
#3

Thanks
Reply
#4

nice
Reply
#5

Nice, and how can it make parkable and lockable?
Reply
#6

Quote:
Originally Posted by DauerDicht
Nice, and how can it make parkable and lockable?
Parkable?
For lockable: https://sampwiki.blast.hk/wiki/Function:...aramsForPlayer
Reply
#7

excuse me but i do not under stand this part:
if the player name matthias is true why does it remove him from the vehicle should it remove if the name amtthias is false? thx for ur help it helped alot
Reply
#8

Because string compare is stupid and returns true if the strings don't match.
That's why you use on commands strcmp("/lol", cmdtext) == 0, so the cmd will execute if strcmp is false
Reply
#9

owwwwww thxxx mate
Reply
#10

Nice work Matt..this is useful for admins!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)