Question: Trigger client event with no client connected / Spawn vehicles in server.lua

Hi there,
im a dev for years now but never touched lua. Also im a bit wondering if i can spawn vehicles from server side lua (im trying to modify esx_eden_garage to spawn cars at server startup at last location(which i already saved in database)).

Is it possible to spawn a vehicle in client script if no client is connected?

Also i dont understand where to see if its a server or client function (in rage wiki). A bit wired at start.
also i found nothing useful in the net

Thanks in advance

No. Who will spawn the vehicle then? That’s right, no one can :wink:

Use the FiveM natives documentation https://runtime.fivem.net/doc/natives/

Anything you can “see” on the client is done on the client (with exception when using OneSync you can spawn/manipulate entities - vehicles, ped - on the server side). But either way, a client must be connected so that someone can actually spawn vehicles. Your server will just tell everyone “Hey, I need someone to spawn this for me”.

No GTA V multiplayer mod is able to spawn vehicles without a client being present. You could however keep track of what has been spawned, what needs to spawn etc when a first client connects. Example [Release] Boot Vehicles v1.3 - Persistent Configured Vehicles!

thanks mate, just for my understanding; will this not trigger every time a user connects and total is one player online (so far so good) but the cars would spawn again and again if only one player connects and disconnects and connects again right? Hope you know what i meant

nope; currently there’s actually no support for entity ‘persistence’ in that sense either, so you don’t have to worry about that at all

that means if last player disconnects the all cars will despawn?
im still on to give it a shot to achieve my goal :smiley: (code is giving a bit trouble but im on)

No game world exists, so yes.

Thanks for this info, makes sense but if you dont have such information p.p
I got my code and it seems to work as it puts out all required informations but car wont spawn.
Also the execution works fine (5s delay after first player spawned).
Client:

RegisterNetEvent('LoadVeh')
AddEventHandler('LoadVeh', function (vehicle,x,y,z,a)
	ESX.Game.SpawnVehicle(vehicle.model, {x,y,z},120, function(callback_vehicle)
        ESX.Game.SetVehicleProperties(callback_vehicle, vehicle)
    end)
    TriggerServerEvent('eden_garage:modifystate', vehicle, false)
end)
AddEventHandler("playerSpawned", function(spawnInfo)
	if GetNumberOfPlayers() == 1 then
		Citizen.Wait(5000)
		print("Loading Vehicles:")
		TriggerServerEvent('LoadVehicleSpawn')
	end
  end)

Server:

RegisterServerEvent('LoadVehicle')
AddEventHandler('LoadVehicle', function()
  MySQL.Async.fetchAll(
    'SELECT * FROM owned_vehicles',
    {},
    function (result)
      for i=1, #result, 1 do
        if result[i].X == 0 then
          print("Loading Vehicle: (ID:".. result[i].id ..") => Skipping due Spawn @0 coordinates" )
        else
          local veh = json.decode(result[i].vehicle)
          print("Loading Vehicle: (ID:".. result[i].id ..") => Spawning (".. veh.model .."/".. veh.plate ..")" .. result[i].id .. " at X:".. result[i].X .."|Y:".. result[i].Y .."|Z:".. result[i].Z .."|A:".. result[i].A )
          TriggerClientEvent('LoadVehicleSpawn', veh,result[i].X,result[i].Y,result[i].Z,result[i].A)
        end
      end
    end)
    print("")
    print("Loading Vehicles: DONE!")
    print("")
end)

OutPut:

Successfully Loaded Identity For Tom Street

Loading Vehicles: DONE!

Loading Vehicle: (ID:1) => Skipping due Spawn @0 coordinates
Loading Vehicle: (ID:2) => Skipping due Spawn @0 coordinates
Loading Vehicle: (ID:3) => Skipping due Spawn @0 coordinates
Loading Vehicle: (ID:4) => Skipping due Spawn @0 coordinates
Loading Vehicle: (ID:5) => Skipping due Spawn @0 coordinates
Loading Vehicle: (ID:6) => Skipping due Spawn @0 coordinates
Loading Vehicle: (ID:7) => Skipping due Spawn @0 coordinates
Loading Vehicle: (ID:8) => Skipping due Spawn @0 coordinates
Loading Vehicle: (ID:9) => Skipping due Spawn @0 coordinates
Loading Vehicle: (ID:10) => Skipping due Spawn @0 coordinates
Loading Vehicle: (ID:11) => Skipping due Spawn @0 coordinates
Loading Vehicle: (ID:12) => Skipping due Spawn @0 coordinates
Loading Vehicle: (ID:13) => Skipping due Spawn @0 coordinates
Loading Vehicle: (ID:14) => Skipping due Spawn @0 coordinates
Loading Vehicle: (ID:15) => Skipping due Spawn @0 coordinates
Loading Vehicle: (ID:16) => Skipping due Spawn @0 coordinates
Loading Vehicle: (ID:17) => Skipping due Spawn @0 coordinates
Loading Vehicle: (ID:18) => Skipping due Spawn @0 coordinates
Loading Vehicle: (ID:20) => Skipping due Spawn @0 coordinates
Loading Vehicle: (ID:21) => Skipping due Spawn @0 coordinates
Loading Vehicle: (ID:22) => Spawning (-1045541610/49ZWO308)22 at X:262.8219909668|Y:-1482.6899414063|Z:28.230600357056|A:230.62100219727
Loading Vehicle: (ID:23) => Spawning (-2112200549/80NRS547)23 at X:771.73797607422|Y:-952.17700195313|Z:25.238700866699|A:182.08399963379
Loading Vehicle: (ID:24) => Spawning (-73653218/27PWB705)24 at X:100.0|Y:100.0|Z:85.0|A:0.0

Output looks quite good… but no car is seen at the coords :confused:
any hints?

The skipped ones are cars withouth positions, i added the if ==0.0 to disable the spawn, they can be pulled out at garage^^

Check what you named your client event and what you are triggering from the server :wink:

@d0p3t yea thats true but also corrected there are no vehicles spawning :C that triggers me
console shows correct spawn 5secs after i spawn.
@d0p3t greetings to NL, loved my times in Nijmegen <3

I’m trying to use the native GetNumberOfPlayers() to trigger an event at first connection :

RegisterNetEvent('esx:playerLoaded')
AddEventHandler('esx:playerLoaded', function(xPlayer)
  	if GetNumberOfPlayers() == 1 then
		TriggerEvent('blabla')
	end
end)

But it always returns 1 for each client loaded in their client console.
Is this a OneSync related limitation or something I did wrong or… ?
Also tried with the native mentioned by Vespura NetworkGetNumConnectedPlayers() but the result remains 1. Don’t understand why.

Example for FirstSpawn
(called only once if the player spawns first after connect, no trigger for next respawns for this player
and the additional “and GetNumberOfPlayers() == 1” checks if he is the first one on the server :slight_smile: )

local FirstSpawn = true -- Global 
AddEventHandler(
  "playerSpawned",
  function(spawnInfo)
    if FirstSpawn and GetNumberOfPlayers() == 1 then
      FirstSpawn = false
      -- [[ Do your shit :) ]]
Citizen.Trace("Yea first player connected to instance. Creating environment!\n")
   
    end
     
  end
)

Edit: its client lua, just sayin :smiley: