ChainsBoy

Car Spawn Thingy Fixed

Feb 27th, 2016
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.12 KB | None | 0 0
  1. addCommandHandler( "car",
  2.     function( playerid, vehicleid )
  3.     {
  4.         local mypos = getPlayerPosition(playerid);
  5.         if( isPointInCircle2D( mypos[0], mypos[1], -575.114868, 1623.623169, 35.0 ))  
  6.         {
  7.             sendPlayerMessage( playerid, "[Server]: No cars on the spawn area!", 239, 51, 64 );
  8.             return 1;
  9.             }
  10.        
  11.         if( isPlayerInVehicle( playerid ) )  
  12.         {    
  13.             sendPlayerMessage( playerid, "[Server]: For a spawn new car you must be on foot!", 239, 51, 64 );    
  14.             return 1;
  15.         }  
  16.        
  17.         // You might want to check if the timer is active in here, if it is then return 1.
  18.         // Currently there is nothing stopping them, except for the above if statements to spawn new vehicles
  19.         // And its better to block spawning rather than destroying the vehicle when spawned
  20.  
  21.         // This variable should be global, aka outside the functions and commands
  22.         // Currently the function blockcarforplayer cant access this
  23.         local myTimer;
  24.  
  25.         local pos = getPlayerPosition( playerid );
  26.         local vehicle = createVehicle( vehicleid.tointeger(), pos[0] + 2.0, pos[1], pos[2] + 1.0, 0.0, 0.0, 0.0 );
  27.  
  28.         // You are calling a function with a timer and checking whether the same timer is active or not
  29.         // That will always return true
  30.         // Unless you call the function with another timer to check if a different timer is active or not
  31.         myTimer = timer( blockcarforplayer, 100, 1, playerid);
  32.  
  33.         sendPlayerMessage( playerid, "[Server]: Car ID: (" +vehicleid.tointeger() + ") has been sucessfully spawned", 0, 255, 0 );
  34.        
  35.     }  
  36.  
  37. );
  38.  
  39.  
  40. function blockcarforplayer(playerid)
  41. {
  42.     // You just created a variable which does not hold anything
  43.     local myTimer;
  44.  
  45.     //local myTimer = timer( blockcarforplayer, 9000, 1, playerid);
  46.        
  47.     // Now you are checking whether that variable is active or not (returns error, IsActive does not exist)
  48.     // And if you are checking if a timer is active in a function called by the same timer, it will always return true
  49.     if( myTimer.IsActive() )
  50.     {
  51.         // You are using playerid, you need the vehicleid (getPlayerVehicle())
  52.         destroyVehicle(playerid);
  53.  
  54.         sendPlayerMessage( playerid, "[Server]: Wait!", 239, 51, 64 );
  55.         return 1;
  56.     }
  57. }
  58.  
  59.  
  60.  
  61.  
  62. /*************** NEW VERSION, Not tested but should work ***********/
  63. // With this we check if CMD is blocked per player
  64. local vehCMDBlock = array(MAX_PLAYERS, 0);
  65. // How long it waits before it unblocks the CMD
  66. local vehCMDBlockTime = 5000; // 5 seconds currently
  67.  
  68. addCommandHandler( "car",
  69.     function( playerid, vehicleid )
  70.     {
  71.         local mypos = getPlayerPosition(playerid);
  72.         if( isPointInCircle2D( mypos[0], mypos[1], -575.114868, 1623.623169, 35.0 ))  
  73.         {
  74.             sendPlayerMessage( playerid, "[Server]: No cars on the spawn area!", 239, 51, 64 );
  75.             return 1;
  76.         }
  77.        
  78.         if( isPlayerInVehicle( playerid ) )  
  79.         {    
  80.             sendPlayerMessage( playerid, "[Server]: For a spawn new car you must be on foot!", 239, 51, 64 );    
  81.             return 1;
  82.         }
  83.        
  84.         // If cmd block is still active
  85.         if( vehCMDBlock[playerid] == 1 ) {
  86.             sendPlayerMessage( playerid, "[Server]: No señior, you no car", 239, 51, 64 );
  87.             return 1;
  88.         }
  89.  
  90.         local pos = getPlayerPosition( playerid );
  91.         local vehicle = createVehicle( vehicleid.tointeger(), pos[0] + 2.0, pos[1], pos[2] + 1.0, 0.0, 0.0, 0.0 );
  92.         sendPlayerMessage( playerid, "[Server]: Car ID: (" +vehicleid.tointeger() + ") has been sucessfully spawned", 0, 255, 0 );
  93.        
  94.         // Block usage of this command
  95.         vehCMDBlock[playerid] = 1;
  96.        
  97.         timer( function() {
  98.             // Unblock usage
  99.             vehCMDBlock[playerid] = 0;
  100.  
  101.             // In here you can also destroy the previous vehicle
  102.             // You need to create another global variable for a player (lastVehicleID ?)
  103.             // Store the id from created vehicle (found inside the  vehicle  variable above)
  104.             // And destroy the vehicle with that ID
  105.         }, vehCMDBlockTime, 1);
  106.        
  107.     }
  108. );
Add Comment
Please, Sign In to add comment