[Tutorial] Creating a simple personal car.
#1

Personal Car:

- First of all, we need to make a variable, at the top of script, under includes/defines we add:

pawn Код:
new Car;
- With this variable, we define the personal car for that player.

- Then, we need to create the car, under OnFilterScriptInit() or OnGameModeInit(). Depends if you are using a Filescript [OnFilterScriptInit()] or if you are using a GameMode [OnGameModeInit()]. And then, we create the car:

pawn Код:
Car = CreateVehicle(Vehicle Model, Float:X, Float:Y, Float:Z, Float:Angle, Color1, Color2, RespawnTime);
Vehicle Model - It's the model of vehicle wich you want to create. All vehicle models can be found right here!

X, Y, Z, Angle - Are the positions of that car. Them can be found by going in game and type /Save, then going in My Documents/GTA San Andreas User Files/SAMP/savedpositions.txt

Color1, Color2 - Are the colors of that car. Them can be found, too, right here!

Respawn Time - Time after which the car will be respawned.



- After creating the car, we'll moving under OnPlayerEnterVehicle(playerid, vehicleid), and we add the code:

pawn Код:
public OnPlayerEnterVehicle(playerid, vehicleid) // public
{ // Open bracket
    new name[MAX_PLAYER_NAME]; // Hrre make a variable for player's name!
    GetPlayerName(playerid, name, sizeof(name)); // Here we gettin' his and, to know if he is leader of car, else will remove him.
    new Car2 = GetPlayerVehicleID(playerid); // Here we define the Car2 variable, and get his car vehicle id;
    if(Car2 == Car) // If Car2 variable it's Car creating by us below.
    { // Open bracket!
        if(strcmp(name, "NameOfPlayer", true)) // Replace "NameOfPlayer" with the owner of car wich you want to be. And if he is owner.
        { // Open bracket
            SendClientMessage(playerid, -1, "Welcome in your vehicle!"); // Send to him message "Welcome in your vehicle!"
        } // Close bracket !
        else // Else, if he isn't owner of that car!
        { // Open bracket !
            SendClientMessage(playerid, -1, "It's not your vehicle!"); // Will send to him message "It's not your vehicle!"
            RemovePlayerFromVehicle(playerid); // He will got removed from vehicle !
        } // Close bracket
    } // Close bracket *2
    return 1; // Returns the value.
} // Closing bracket
Reply
#2

First problem:
Only one hardcoded player (his name needs to be defined in the script) can own a car, and nobody else has that possibility?
And to change the owner of that single vehicle, you'll have to edit the script, recompile and restart the server.
You need an array to make it work for all players, and add code to set ownership of the vehicle in-game.

Second problem:
OnPlayerEnterVehicle is called whenever a player is NEAR a vehicle and presses the ENTER key.
Then the player's character will start moving towards the vehicle and can't be removed from the vehicle as he's not yet inside of it, your code won't work.
He won't get ejected from the vehicle, you'll only get the message.
Use OnPlayerStateChange instead.
Reply
#3

beside it is hard coded, I don't think that you need to comment every line of code
but still, its nice to help some rookies
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)