Advertisement
Guest User

Untitled

a guest
May 26th, 2015
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.62 KB | None | 0 0
  1. local myThread = ScriptThread ( "carspawn" )
  2.  
  3. function myThread:Run()
  4.     while self:IsRunning() do
  5.         if IsKeyDown ( F4 ) then
  6.             setVehicleModel ( VEHICLE_AIRBUS) --spawn the new car where the old one originally was
  7.         end
  8.         if IsKeyDown ( F11 ) then
  9.             setVehicleModel ( VEHICLE_BLISTA ) --spawn the new car where the old one originally was
  10.         end
  11.         if IsKeyDown ( VK_OEM_5 ) then
  12.             setVehicleModel ( VEHICLE_BLISTA2 ) --spawn the new car where the old one originally was
  13.         end
  14.         if IsKeyDown ( F13 ) then
  15.             setVehicleModel ( VEHICLE_BLISTA3 ) --spawn the new car where the old one originally was
  16.         end
  17.         if IsKeyDown ( F14 ) then
  18.             setVehicleModel ( VEHICLE_BTYPE ) --spawn the new car where the old one originally was
  19.         end
  20.         self:Wait(50)
  21.     end
  22. end
  23.  
  24. --spawning script
  25. function setVehicleModel ( vehicle, model )
  26.     local position = vehicle:GetPosition()--position
  27.     local rotation = natives.ENTITY.GET_ENTITY_ROTATION ( vehicle.ID, 0 )--rotation
  28.     local velocity = vehicle:GetVelocity()--speed
  29.     local heading = vehicle:GetHeading()
  30.     local occupants = getVehiclePassengers ( vehicle )--passengers in format { { -1, playerid }, { 0, false } }
  31.     local oldCarPosition = vehicle:GetOffsetVector ( 0, 0, 5 )--new coordinates for the old car to gtfo temporarily
  32.     vehicle:SetPosition ( oldCarPosition.x, oldCarPosition.y, oldCarPosition.z )--move the old car
  33.    
  34.     streaming.RequestModel ( model )--request new car model
  35.     local newCar = game.CreateVehicle ( model, position )--spawn the new car where the old one originally ways
  36.     streaming.ReleaseModel ( model )--release the request
  37.     newCar:SetVelocity ( velocity )--set the speed of the new car using the vector from GetVelocity
  38.     natives.ENTITY.SET_ENTITY_ROTATION ( vehicle.ID, rotation.x, rotation.y, rotation.z, 1, true )--set the rotation to the old rotation
  39.     newCar:SetHeading(heading)
  40.     for i=1,#occupants do--for each object in my table
  41.         if occupants[i][2] then--if we actually got a player in that seat
  42.             natives.AI.CLEAR_PED_TASKS_IMMEDIATELY ( occupants[i][2] )--remove them from the car
  43.         end
  44.     end
  45.     wthread:Wait(50)
  46.     vehicle:SetPosition ( 0, 0, 0 )--move the car to no man's land for game engine deletion
  47.     removeVehicle ( vehicle.ID )
  48.     natives.PED.SET_PED_INTO_VEHICLE ( LocalPlayer().ID, newCar.ID, -1 )--move local player into the new vehicle
  49.     newCar:SetEngineState ( true )
  50. end
  51.  
  52. function getVehiclePassengers ( vehicle )
  53.     if natives.ENTITY.IS_ENTITY_A_VEHICLE ( vehicle.ID ) then
  54.         local maxPassengers = natives.VEHICLE.GET_VEHICLE_MAX_NUMBER_OF_PASSENGERS ( vehicle.ID ) - 2
  55.         local occupants = {}
  56.         for i=-1,maxPassengers do
  57.             local seatFree = natives.VEHICLE.IS_VEHICLE_SEAT_FREE ( vehicle.ID, i )
  58.             local occupant = false
  59.             if not seatFree then
  60.                 occupant = natives.VEHICLE.GET_PED_IN_VEHICLE_SEAT ( vehicle.ID, i )
  61.             end
  62.             table.insert ( occupants, { i, occupant } )
  63.         end
  64.         return occupants
  65.     elseif natives.ENTITY.IS_ENTITY_AN_ENTITY ( vehicle.ID ) then
  66.         print ( "getVehiclePassengers - Variable 'vehicle' was not a vehicle entity." )
  67.         return false
  68.     else
  69.         print ( "getVehiclePassengers - Variable 'vehicle' was not a vehicle entity nor entity." )
  70.         return false
  71.     end
  72. end
  73.  
  74. function removeVehicle ( vehicle )
  75.     c_veh = CMemoryBlock ( 4 )
  76.     c_veh:WriteDWORD32 ( 0, vehicle )
  77.     natives.ENTITY.DELETE_ENTITY ( c_veh )
  78.     c_veh:Release()
  79.     wthread:Wait ( 0 )
  80. end
  81.  
  82. function myThread:OnError()
  83.     self:Reset()
  84. end
  85.  
  86. myThread:Register()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement