Vehicle Spawning

Hello!

I was wondering if there is any way of getting the /car command back or if someone could help me make one. I found the feature really useful and a lot more efficient rather than doing it through a trainer.

Client

RegisterNetEvent('VehicleSpawn')
AddEventHandler('VehicleSpawn', function(vehicleName)
  local myPed = GetPlayerPed(-1)
  local player = PlayerId()
  local vehicle = GetHashKey(vehicleName)
  RequestModel(vehicle)
  while not HasModelLoaded(vehicle) do
    Wait(1)
  end
  local coords = GetOffsetFromEntityInWorldCoords(GetPlayerPed(-1), 0, 5.0, 0)
  local spawned_car = CreateVehicle(vehicle, coords, GetEntityHeading(myPed), true, false)
  SetVehicleOnGroundProperly(spawned_car)
  SetModelAsNoLongerNeeded(vehicle)
  Citizen.InvokeNative(0xB736A491E64A32CF,Citizen.PointerValueIntInitialized(spawned_car))
end)

Citizen.CreateThread(function()
  while true do
    Wait(1)
  end
end)
RegisterNetEvent('VehicleLivery')
AddEventHandler('VehicleLivery', function()
	local Veh = GetVehiclePedIsIn(GetPlayerPed(-1))

	SetVehicleLivery(Veh, livery) --CHANGE livery(id)
	SetVehicleExtra(Veh, extraId, toggle) -- CHANGE extraId, toggle(true/false)
end)

Server

AddEventHandler('chatMessage', function(source, n, message)
  local args = stringsplit(message, " ")
  if (args[1] == "/spawn") then
    CancelEvent()
    if (args[2] ~= nil) then
      local playerID = tonumber(source)
      local vehicleName = tostring(args[2])
      TriggerClientEvent('VehicleSpawn', playerID, vehicleName)
    else
      local event = 'chatMessage'
      local eventTarget = source
      local messageSender = "SYSTEM"
      local messageSenderColor = {200, 0, 0}
      local message = "Usage: /spawn <Vehicle_Name>  (for example /spawn polmav)"
      TriggerClientEvent(event, eventTarget, messageSender, messageSenderColor, message)
    end
  end
end)

function stringsplit(inputstr, sep)
    if sep == nil then
        sep = "%s"
    end
    local t={} ; i=1
    for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
        t[i] = str
        i = i + 1
    end
    return t
end

Behave and enjoy

You’re a legend.

thank you!

Any time… I feel like we’ve had this conversation before :wink: