[ESX] Spawn cars on server startup (already coded but spawnbug)

Hi Guys,

i modified a script to store vehicles x,y,z,a(angle/heading) every x seconds in mysql.
This works perfectly and is tested.
Then i wrote a bit to load them on startup which seems to work (like print shows) but cars are NOT spawned… no errors or warnings shown in console.

Code: Client & Server
Codeedit: I uppercased save function.

Do you have any ideas? I found nothing related to this idk… my goal is to load all vehicles on serverstartup and to disable veh despawn if player disconnects (not coded yet as i do step by step)

Thanks for any hints and merry x-mas to you

your (loop , it shouldnt be a loop lol ) will trigger your function after 360000000 of client will load.
and it will re-trigger the event within 360000000.

remove the loop

–while true do
CODE
–end

thanks mate, makes sense :slight_smile:
saw this code for “startup” at a git check in one of the esx modules
do you have any idea regarding the spawn?

my code is from the script itself.
Also in advanced garage is no feature like spawn on startup at last location …

youre grabbing data from database via a callback into client , you set that as a function into client side.
then open a thread once , trigger the client sided event. thats all.
for example

function SpawnVehOne()
    modelRequest(Config.Zones.Veh_One.Model)
	VehOne = CreateVehicle(Config.Zones.Veh_One.Model, Config.Zones.Veh_One.X,Config.Zones.Veh_One.Y,Config.Zones.Veh_One.Z,Config.Zones.Veh_One.Heading, false, true)
	SetVehicleOnGroundProperly(VehOne)
	local vehicleProps = NYAM.Game.GetVehicleProperties(VehOne)
    local vehHash = Config.Zones.Veh_Two.Model
    local vehName  = GetDisplayNameFromVehicleModel(vehHash)
    local newPlate = 'SHOWCASE' .. string.upper(NYAM.GetRandomString(1))
    vehicleProps.plate = newPlate
	SetVehicleNumberPlateText(VehOne, newPlate)
	TriggerEvent('nyam_hotwire:forceTurnOver', VehOne)
	Citizen.Wait(5000)
	if DoesEntityExist(VehOne) then
		FreezeEntityPosition(VehOne, true)
	end
end

this is how a create a vehicle from configs , for my car show room

and here you can see the thread , as it spawn em and clear few rnd npcs around

Citizen.CreateThread(function()
	SpawnVehOne()
	SpawnVehTwo()
	SpawnVehThree()
	SpawnVehFour()
	SpawnVehFive()
	SpawnVehSix()
	SpawnVehSeven()
	SpawnVehEight()
	SpawnVehNine()
	while true do
		Wait(150)
		ClearAreaOfPeds(-1153.03, -1761.63, 4.0, 20.0, 0)
		ClearAreaOfVehicles(-1153.03, -1761.63, 4.0, 20.0, false, false, false, false, false)
	end
end)

Appreciate your example; i got my results from db so i keep it dynamic. If i translate your function i will end up more or less at the same code i wrote (i think).
I used ESX. vehicle create but yea.

im new to lua and to fivem. 12 years ago i was a very powerful coder at pawn (samp multiplayer written in c) but lua is kinda new to me with this way and also documentation is mh well :smiley: you know^^

//edit yea got it, you do it server-side^^ ill try thanks for the hint

same , was for java lineage 2 years ago :stuck_out_tongue: :stuck_out_tongue: and new to fivem.
also if you know C# go for it lol :smiley: fivem supports it , more stable bla bla bla

still struggling :c

i had a facepalm in client:

ESX.Game.SpawnVehicle(vehicle.model, {},120, function(callback_vehicle)

changed it to:

ESX.Game.SpawnVehicle(vehicle.model, {x,y,z+1},120, function(callback_vehicle)

i tried with createvehicle but failed :C -.-

Hi, I’m trying to do the same in a car dealership but I can’t, could you pass me the complete code? Thank you.

1 Like

my example was very noobish , i recommend you to not try with the same way :stuck_out_tongue: