[Release] Car spawning via chat commands

#5

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

Thanks,
Caesar

#6

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

#7

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
    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(self, delimiter)
  local a = self:Split(delimiter)
  local t = {}

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

  return t
end

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

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)

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:”.

1 Like
[Help] /pv (personal vehicle) execute auto with a Menu (Mysql..)
Spawning non-car vehicules
Clientside/Serverside
#8

Thank you very much!

#9

Works perfectly. Thank you

#10

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

#11

i have error with the script

#12

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
#13

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?

#14

Replace function in server script:

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
4 Likes
Command arguments?
#15

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

2 Likes
#16

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

#17

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

#18

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

Very simple /car command
#19

Here’s an updated one Very simple /car command

#20

How to install the script?

#21

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

#22

Add ace perms to it so you can control who is able to use the cmd.

#23

i’ve read many times that script and that solved but someone still spawn vehicle, when they try to use command chat for spawn vehicle, in my screen said ( System : spawn is not valid command ) but the vehicle still spawn

#24

Its not a script its a client native found here (FiveM Natives)

If you set it up correctly it wont let anyone spawn them in unless you want them to.
You also need to make sure there is no other ways to spawn the cars.