[Tutorial] Repairing vehicle when pressing a key.
#1

Introduction


Hello Everyone, i am DarkSkull, and this is my first ever Tutorial. In this tutorial we will be creating a vehicle repair system where when pressed a button, it will repair the vehicle for you.

P.S: I am a Newbie, So please forgive if i make any mistakes.

So, Let's get started.!

Assigning a Key


We want to make it so that when the player presses the button, we will trigger the function.

So to make that work, we will use the OnPlayerKeyStateChange() function.

PHP код:
public OnPlayerKeyStateChange(playeridnewkeysoldkeys)
{
    return 
1;

Now we want to choose a key. You can find all the keys here. For the purpose of this tutorial, i will use the KEY_SUBMISSION, which is by the default the 'Number 2' key on the keyboard.

Scripting the Function


Now, Let's get started on the function itself. So, we want to check if the player press our key, So,

PHP код:
if ((newkeys KEY_SUBMISSION) && !(oldkeys KEY_SUBMISSION)){

Here, we are checking if the player has pressed the key and if it is set in newkeys, meaning if it is just being pressed, and not in old keys, meaning if it was being held before. If that statement is true, then we want continue with our code.

PHP код:
if ((newkeys KEY_SUBMISSION) && !(oldkeys KEY_SUBMISSION)){
        if(
IsPlayerInAnyVehicle(playerid)){
            
        }
    } 
Next, we are checking if the player pressing the key is in any vehicle, if it is true, we run the code other wise we don't.

PHP код:
if ((newkeys KEY_SUBMISSION) && !(oldkeys KEY_SUBMISSION)){
        if(
IsPlayerInAnyVehicle(playerid)){
            
RepairVehicle(GetPlayerVehicleID(playerid));
            
        }
    } 
So, if the player is in the vehicle, and he/she has pressed the key, then we repair the vehicle. Repairing the vehicle takes one parameter, vehicle id. Therefore, we get the vehicle id using the method GetPlayerVehicleId(); and passing the players id into it. And That's It !.
More Stuffs


If you want, you can even send a message and play a sound, when the vehicle is being repaired like so.

PHP код:
if ((newkeys KEY_SUBMISSION) && !(oldkeys KEY_SUBMISSION)){
        if(
IsPlayerInAnyVehicle(playerid)){
            
RepairVehicle(GetPlayerVehicleID(playerid));
            
PlayerPlaySound(playerid11330.00.00.0);
            
SendClientMessage(playerid0x33AA33AA"[INFO]: Your vehicle has been repaired");
            
        }
    } 
And you can also add an error message when the player presses two and he is not in a vehicle.

PHP код:
if ((newkeys KEY_SUBMISSION) && !(oldkeys KEY_SUBMISSION)){
        if(
IsPlayerInAnyVehicle(playerid)){
            
RepairVehicle(GetPlayerVehicleID(playerid));
            
PlayerPlaySound(playerid11330.00.00.0);
            
SendClientMessage(playerid0x33AA33AA"[INFO]: Your vehicle has been repaired");
            
        }else {
        
SendClientMessage(playerid0xFF0000FF"[ERROR: You're not in a vehicle]");
    }
    } 
Tweak it around, and use it to your liking.!

Final Tutorial Code

PHP код:
public OnPlayerKeyStateChange(playeridnewkeysoldkeys)
{
    if ((
newkeys KEY_SUBMISSION) && !(oldkeys KEY_SUBMISSION)){
        if(
IsPlayerInAnyVehicle(playerid)){
            
RepairVehicle(GetPlayerVehicleID(playerid));
            
        }
    }
    return 
1;

Don't just copy paste, read, understand and use it.

If you want to read more about OnPlayerKeyStateChange(), then check out the Wiki.


Credits


SA:MP Wiki - OnPlayerKeyStateChange()
SA:MP Wiki - SoundID
Reply
#2

You could shorten it a bit, removing the need to use IsPlayerInAnyVehicle. But otherwise it looks okay.
pawn Код:
if(RepairVehicle(GetPlayerVehicleID(playerid)))
{
    PlayerPlaySound(playerid, 1133, 0.0, 0.0, 0.0);
    SendClientMessage(playerid, 0x33AA33AA, "[INFO]: Your vehicle has been repaired");
}
else
{
    SendClientMessage(playerid, 0xFF0000FF, "[ERROR: You're not in a vehicle]");
}
Reply
#3

Thanks +Your first post !
Reply
#4

Quote:
Originally Posted by Vince
Посмотреть сообщение
You could shorten it a bit, removing the need to use IsPlayerInAnyVehicle. But otherwise it looks okay.
pawn Код:
if(RepairVehicle(GetPlayerVehicleID(playerid)))
{
    PlayerPlaySound(playerid, 1133, 0.0, 0.0, 0.0);
    SendClientMessage(playerid, 0x33AA33AA, "[INFO]: Your vehicle has been repaired");
}
else
{
    SendClientMessage(playerid, 0xFF0000FF, "[ERROR: You're not in a vehicle]");
}
Oh yeah, guess i could have..
Reply
#5

Quote:
Originally Posted by JeevanJyothish
Посмотреть сообщение
Thanks +Your first post !
Hey, Thanks
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)