Liberty Unleashed

Scripting => Script Snippets => Topic started by: iulix2012 on May 26, 2013, 12:02:32 pm

Title: lock/unlock script
Post by: iulix2012 on May 26, 2013, 12:02:32 pm
Code: [Select]
else if( cmd == "lock"){
  if ( player.Vehicle ){
MessagePlayer( "Your vehicle is locked!", player );
 player.Vehicle.Locked = true;
}else{
MessagePlayer( "You must be in a vehicle to use this command!", player );
}
}
else if( cmd == "unlock"){
  if ( player.Vehicle ){
MessagePlayer( "Your vehicle is unlocked!", player );
 player.Vehicle.Locked = false;
}else{

MessagePlayer( "You must be in a vehicle to use this command!", player );
}
}
Title: Re: lock/unlock script
Post by: Thijn on May 26, 2013, 01:02:55 pm
Moved and added code tags.
Title: Re: lock/unlock script
Post by: NC on May 26, 2013, 07:34:50 pm
1. Use TABulators.
2. You could add a check for the lock (unlock) command if the vehicle is already closed (opened). Why should someone close a closed car :)?
Title: Re: lock/unlock script
Post by: Yamaza on July 02, 2013, 09:39:01 pm
I have a more efficient script for this...:

Code: [Select]
function onPlayerCommand( pPlayer, szCommand, szArgs )
{
if ( szCommand == "lock" )
{
if ( !pPlayer.Spawned )
{
MessagePlayer( "You're not spawned.", pPlayer );
return;
}

if ( !pPlayer.Vehicle )
{
MessagePlayer( "You're not in a vehicle.", pPlayer );
return;
}

if ( pPlayer.VehicleSeat > 0 )
{
MessagePlayer( "You're not the driver on this vehicle.", pPlayer );
return;
}

if ( !szArgs )
{
MessagePlayer( "Locks or unlocks the doors of your vehicle. Usage: /" + szCommand + " < ON / OFF >", pPlayer );
return;
}

local veh = pPlayer.Vehicle;

switch ( szArgs )
{
case "on":
if ( veh.Locked )
{
MessagePlayer( "This vehicle is already locked.", pPlayer );
return;
}

veh.Locked = true;
MessagePlayer( "You have locked the doors of the vehicle.", pPlayer );
break;

case "off":
if ( !veh.Locked )
{
MessagePlayer( "This vehicle is already unlocked.", pPlayer );
return;
}

veh.Locked = false;
MessagePlayer( "You have unlock the doors of the vehicle.", pPlayer );
break;

default:
MessagePlayer( "Unknown usage! Usage: /" + szCommand + " < ON / OFF >", pPlayer );
break;
}
}

return 1;
}

Credits: Me
Title: Re: lock/unlock script
Post by: Thijn on July 04, 2013, 05:52:47 pm
Why is it more efficient when it has more lines? :P
Title: Re: lock/unlock script
Post by: Yamaza on July 05, 2013, 04:08:39 pm
Because the code runs faster, without else and else if. This more optimized. I checked it with a test of milliseconds. :P