Vice City: Multiplayer

Server Development => Scripting and Server Management => Snippet Showroom => Topic started by: PsyChO_KiLLeR on Mar 05, 2015, 06:07 AM

Title: Hotkey Fix Vehicle
Post by: PsyChO_KiLLeR on Mar 05, 2015, 06:07 AM
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
Title: Re: Hotkey Fix Vehicle
Post by: Thijn on Mar 05, 2015, 06:53 AM
Really? All you give credits to is yourself? What about these people who basically made this snippet: http://forum.vc-mp.org/?topic=311
Title: Re: Hotkey Fix Vehicle
Post by: PsyChO_KiLLeR on Mar 05, 2015, 06:55 AM
well credits also goes to karan but i am not using there function
Title: Re: Hotkey Fix Vehicle
Post by: Fuzzie on Mar 05, 2015, 07:07 AM
This should be in the Snippet Showroom
Title: Re: Hotkey Fix Vehicle
Post by: PsyChO_KiLLeR on Mar 05, 2015, 07:10 AM
well we have to move it
Title: Re: Hotkey Fix Vehicle
Post by: rObInX on Mar 05, 2015, 07:54 AM
You'll get error on else if.
Title: Re: Hotkey Fix Vehicle
Post by: PsyChO_KiLLeR on Mar 05, 2015, 08:23 AM
nope it is 100℅ Tested
Title: Re: Hotkey Fix Vehicle
Post by: rObInX on Mar 05, 2015, 08:44 AM
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.
Title: Re: Hotkey Fix Vehicle
Post by: Kratos_ on Mar 05, 2015, 09:18 AM

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.
Title: Re: Hotkey Fix Vehicle
Post by: rObInX on Mar 05, 2015, 09:26 AM
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 );
  }
}
Title: Re: Hotkey Fix Vehicle
Post by: Kratos_ on Mar 05, 2015, 10:17 AM

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:-


This can be done like this :-


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);
}
Title: Re: Hotkey Fix Vehicle
Post by: 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.
Title: Re: Hotkey Fix Vehicle
Post by: MacTavish on Mar 05, 2015, 11:01 AM
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);
}
Title: Re: Hotkey Fix Vehicle
Post by: rObInX on Mar 05, 2015, 04:36 PM
Betzone, your function can crash the server if no player is found.
I've already made a stable one above.
Title: Re: Hotkey Fix Vehicle
Post by: Thijn on Mar 05, 2015, 05:38 PM
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.
Title: Re: Hotkey Fix Vehicle
Post by: rObInX on Mar 05, 2015, 08:10 PM
Quote from: Thijn on Mar 05, 2015, 05:38 PM
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.

Copy-paste fail, my bad.
Would be careful next time!

Thanks for pointing out.
Title: Re: Hotkey Fix Vehicle
Post by: PsyChO_KiLLeR on Mar 06, 2015, 06:52 AM
are u saying to me? or beztone?
Title: Re: Hotkey Fix Vehicle
Post by: Sebastian on Mar 06, 2015, 11:33 AM
Quote from: PsyChO_KiLLeR on Mar 06, 2015, 06:52 AMare u saying to me? or beztone?

Yo, bLiNd_KiLLeR.
As you couldn't see, r0binX replied to Thijn's reply.
Title: Re: Hotkey Fix Vehicle
Post by: PsyChO_KiLLeR on Mar 06, 2015, 01:58 PM
ok sorry