Struggling to convert default /car spawn command from client-side to server-side

I can’t really find what’s wrong here as I am getting no feedback, I type the command and it doesn’t return anything but doesn’t work either.

Client-Side

RegisterNetEvent('ac:car')
AddEventHandler('ac:car', function(args)
    local vehicleName = args[1]

    if not IsModelInCdimage(vehicleName) or not IsModelAVehicle(vehicleName) then
        TriggerEvent('chat:addMessage', {
            args = { 'It might have been a good thing that you tried to spawn a ' .. vehicleName .. '. Who even wants their spawning to actually ^*succeed?' }
        })

        return
    end
    RequestModel(vehicleName)

    while not HasModelLoaded(vehicleName) do
        Wait(500) -- often you'll also see Citizen.Wait
    end
    local playerPed = PlayerPedId() -- get the local player ped
    local pos = GetEntityCoords(playerPed) -- get the position of the local player ped
    local vehicle = CreateVehicle(vehicleName, pos.x, pos.y, pos.z, GetEntityHeading(playerPed), true, false)
    SetPedIntoVehicle(playerPed, vehicle, -1)
    SetEntityAsNoLongerNeeded(vehicle)
    SetModelAsNoLongerNeeded(vehicleName)
    TriggerEvent('chat:addMessage', {
		args = { 'You spawned ^*' .. vehicleName .. '!' }
	})
end)

Server-Side

RegisterCommand('car', function(source, args, rawCommand)
 if IsPlayerAceAllowed(source, "admincommands") then
  TriggerClientEvent('ac:car', args[1], args)
 else
  print("You're not an admin.")
 end
end)

This might be solution :
Server side :

TriggerClientEvent('ac:car', source, args[1])
1 Like

Thanks @RiskyShot17 for your interest but unfortunately I am now getting a new error:

SCRIPT ERR0R: @admincommands/client.lua:40: attempt to concatenate a nil value (local ‘vehicleName’)

Line 40 is:

args = { 'It might have been a good thing that you tried to spawn a ' .. vehicleName .. '. Who even wants their spawning to actually ^*succeed?' }

I have replaced this sentence with “Spawning failed” and now it says Spawning failed as coded to but the vehiclename is actually correct and I’ve tried with more than 1. (Banshee, Comet, etc.)

EDIT:
TriggerClientEvent(‘ac:car’, source, args)
I did this and now it works. Thanks @RiskyShot17

1 Like