[Release] Car spawning via chat commands

Hi, I am trying to write a simple script to spawn cars without trainer, using /spawn xxx in chat. I am pretty new to Lua so be gentle.

I think I have working server side script to capture the desired car name from chat. I also have client script that can spawn cars if the model hash is known.

My question: how do I share or send the string from the server that has the desired vehiclename to the client where the car is spawned? Or is this the wrong way to go?

1 Like

There’s a good example how to do commands and networking here

But networking on the server is essentially TriggerClientEvent(Event,ClientId,...)
    If ClientId is -1 it’s broadcasted to all players
    ... Is a vararg meaning you can have any amount of arguments
Clientside is a bit more annnoying as you also have to register it with RegisterNetEvent(Event)
After that you just add a normal event handler AddEventHandler(Event,function(...) end

In the end you should have something like this

-- Server
	if Msg:sub(1,6) == "/spawn" then
-- Client
	-- Code
1 Like

Thanks to you I solved this problem. Thank you so much! I hope threads like this help others who use the search here.

It’s hard to learn the ropes. Reading up on Lua helps, as does studying the natives and looking at other scripts. But sometimes you are still left with a gap, trying to figure out what native does or what the parameters mean, etc.

1 Like

Is there anyway you could upload the script you got working? I can’t seem to figure it out, unfortunately.


make a new folder, call it whatever you want, then just copy the lines into a new file named “sv_spawn.lua” then paste, create a new file name it resources, in there add
"server_script ‘sv_spawn.lua’ " then make sure the folder in added to the cit-mp.yml to the autoresources

CaesarNero, I hoped someone who knows what they were doing might give you some completed script. I have something working but it’s embedded in a bigger script that I am playing with. When I extract the /spawn vehicle section however it doesn’t act like I expect. It seems to need a loop in the client but I’m not sure why. Anyway, you can use this below until someone does it properly – I think it will work.

Create a folder named ‘Spawn’ in your server ‘resources’ folder and then create these files. First put the following into a file named ‘sv_spawn.lua’:

AddEventHandler('chatMessage', function(source, n, message)
  local args = stringsplit(message, " ")
  if (args[1] == "/spawn") then
    if (args[2] ~= nil) then
      local playerID = tonumber(source)
      local vehicleName = tostring(args[2])
      TriggerClientEvent('VehicleSpawn', playerID, vehicleName)
      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)

function stringsplit(self, delimiter)
  local a = self:Split(delimiter)
  local t = {}

  for i = 0, #a - 1 do
     table.insert(t, a[i])

  return t

Second put the following in a file named ‘cl_spawn.lua’:

AddEventHandler('VehicleSpawn', function(vehicleName)
  local myPed = GetPlayerPed(-1)
  local player = PlayerId()
  local vehicle = GetHashKey(vehicleName)
  while not HasModelLoaded(vehicle) do
  local coords = GetOffsetFromEntityInWorldCoords(GetPlayerPed(-1), 0, 5.0, 0)
  local spawned_car = CreateVehicle(vehicle, coords, GetEntityHeading(myPed), true, false)

  while true do

Third create your ‘__resource.lua’ file in the same folder with the following:

client_script "cl_spawn.lua"
server_script "sv_spawn.lua"

And finally, in the citmp-server.yml file in your main server folder, add “- spawn” to the end of your list of “AutoStartResources:”.


Thank you very much!

Works perfectly. Thank you

How to make spawn only one car model with one drive?

i have error with the script

Hello there,

I was wondering if you could help to add a permission in this code because i’ve tried but it doesnt work.

Thanks in advance

1 Like

Hello! I’ve been using this script in my CitizenMP Server and it’s working fine but when I try it in FX Server it’s not working!! Any clues why it’s not working?

Replace function in server script:

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

I tried this for my local FX test server and keep getting error when connecting to server “could not load resource spawn”


i fallowed ur step but didnt still spawn car is there anything els i should do to the script ?

ME TOO lol i did the same thing that this guy said

hi bro. i dont have the citmp-server.yml file… what do i need to do?

Here’s an updated one Very simple /car command

How to install the script?

hey mate !! do you have any solution about remove command (SPAWN) in chat, coz someone can (/spawn khanjali) in my server !! thx