Hotkey Fix Vehicle

Started by PsyChO_KiLLeR, Mar 05, 2015, 06:07 AM

Previous topic - Next topic

PsyChO_KiLLeR

Well I Create a HotKey of Fixing A vehicle its soo easy but its for newbies

Add this onScriptLoad
P <- BindKey(true, 0x50,0,0);


Add this on KeyDown
if( key == P )
{
     local veh = player.Vehicle;
if ( !veh ) MessagePlayer( "You must be in a Vehicle.", player );
else if ( veh.Health == 1000 ) MessagePlayer( "[Dod-Error] There is no need to fix your vehicle.", player );
else
{
         NewTimer( "Fix", 3000, 1, player.ID );
               MessagePlayer( ">> Fixing your Vehicle in 3 secs..", player );
}
  }



Add this on Functions.nut

function Fix(player)
{
local p = FindPlayer( player );
if (p)
{
    p.Vehicle.Fix();
    MessagePlayer( "Successfully fixed your vehicle.", p );
}
}



That it


Credits: Goes to me

Thijn

Really? All you give credits to is yourself? What about these people who basically made this snippet: http://forum.vc-mp.org/?topic=311

PsyChO_KiLLeR

well credits also goes to karan but i am not using there function

Fuzzie

This should be in the Snippet Showroom

PsyChO_KiLLeR

well we have to move it

rObInX

You'll get error on else if.

PsyChO_KiLLeR

nope it is 100℅ Tested

rObInX

#7
Quote from: PsyChO_KiLLeR on Mar 05, 2015, 08:23 AMnope it is 100℅ Tested

I don't think so as server cannot find veh.Health because veh doesn't exists.

And also, learn to give credits.

Kratos_


To fix a vehicle through fix command, that vehicle must be streamed to the player.

if ( !veh ) MessagePlayer( "You must be in a Vehicle.", player );    // will execute if vehicle isn't streamed to the player.
else if ( veh.Health == 1000 ) MessagePlayer( "[Dod-Error] There is no need to fix your vehicle.", player );  // Dependent & will be negation of above condition. Only if vehicle streamed to the player, it will execute



if ( !veh ) MessagePlayer( "You must be in a Vehicle.", player );    // will execute if vehicle isn't streamed to the player.
if ( veh.Health == 1000 ) MessagePlayer( "[Dod-Error] There is no need to fix your vehicle.", player );  // Independent & thus in no relation to the above condition. No matter whether vehicle in stream to the player or not, it will execute. Will throw error in case vehicle isn't streamed to the player.

His code will execute.
In the middle of chaos , lies opportunity.

rObInX

#9
I just checked that, my bad.
But: http://oi57.tinypic.com/2i7vgcw.jpg


Better Code:

function onKeyDown( player, key )
{
   switch(key)
   {
      case P:
      if(player.Vehicle)
        {
local veh = player.Vehicle;
                if ( veh.Health == 1000 ) MessagePlayer( "[Dod-Error] There is no need to fix your vehicle.", player );
                else NewTimer( "Fix", 3000, 1, player.ID );
        }
       break;
    }
}

function Fix(ID)
{
   local p = FindPlayer( ID );
   if ((p) && (p.Vehicle)) // Check if player have a vehicle.
   {
     p.Vehicle.Fix();
     MessagePlayer( "Successfully fixed your vehicle.", p );
  }
}

Kratos_


Dany , he is passing player.ID as an argument to the function Fix . Thereafter he got the player Instance through FindPlayer but haven't checked that whether vehicle is streamed to player or not thatswhy you saw that Index Fix doesn't exist in your console .
Can you see that he is checking that condition of streaming 2 times ?

local veh = player.Vehicle;   // This Condition in command
if ((p) && (p.Vehicle)) // The same condition in fix function

Actually , I thought that he had already checked it in cmd . This whole system could be made much shorter by passing player instance as the argument cuz vehicle streaming condition require the player Instance not the player ID .
Basically he is doing this:-

  • player -> player.vehicle -> player.ID -> p -> p.vehicle ( No checkingup the conditon ) -> p.vehicle.fix();      // [ 6 Steps ]

This can be done like this :-

  • player -> player.Vehicle ( Exist?) -> player.vehicle.fix();    // [ 3 Steps ]

The whole system would be even small :-

if( key == P )
{
 local veh = player.Vehicle;
if ( !veh ) MessagePlayer( "You must be in a Vehicle.", player );
else if ( veh.Health == 1000 ) MessagePlayer( "[Dod-Error] There is no need to fix your vehicle.", player );
else
{
         NewTimer( "Fix", 3000, 1, player );  // Passing the player Instance as the Argument
               MessagePlayer( ">> Fixing your Vehicle in 3 secs..", player );
}
  }



function Fix(player)
{
    player.Vehicle.Fix();  // This function's cmd have checked already whether vehicle is streamed or not. All we have to do is fixing that vehicle.
    MessagePlayer( "Successfully fixed your vehicle.", player);
}
In the middle of chaos , lies opportunity.

rObInX

#11
QuoteDany , he is passing player.ID as an argument to the function Fix . Thereafter he got the player Instance through FindPlayer but haven't checked that whether vehicle is streamed to player or not thatswhy you saw that Index Fix doesn't exist in your console .

I know, that is why I made a stable one.


http://forum.vc-mp.org/?topic=269.msg1477#msg1477
Server will crash if you do so.

MacTavish

Quote from: rObInX on Mar 05, 2015, 10:41 AM
QuoteDany , he is passing player.ID as an argument to the function Fix . Thereafter he got the player Instance through FindPlayer but haven't checked that whether vehicle is streamed to player or not thatswhy you saw that Index Fix doesn't exist in your console .

I know, that is why I made a stable one.


http://forum.vc-mp.org/?topic=269.msg1477#msg1477
Server will crash if you do so.
I've also notice if we made timer like this NewTimer( "Fix", 3000, 1, player ); This will crash server cause of instance but this wont crash server NewTimer( "Fix", 3000, 1, player.ID );
So in fix function will be like this
function Fix(id)
{
local player = FindPlayer( id);
if(player.Vehicle)
{
player.Vehicle.Fix();
MessagePlayer("Vehicle Fixed",player);
}
else MessagePlayer("You Have To Be In A Vehicle To Fix",player);
}

Grand Hunting Project
Join #SLC, #KAKAN, #Doom, #GHP @LUnet

Retired VC:MP Player/Scripter :P

rObInX

#13
Betzone, your function can crash the server if no player is found.
I've already made a stable one above.

Thijn

Quote from: rObInX on Mar 05, 2015, 04:36 PMBetzone, your function can crash the server if no player is found.
I've already made a stable one above.
While you're right about Betzone's code, your code wont work either. You've got an elseif without any previous if statement.