How to preload models during loading

Hi all,

I’ve got a small issue, being that my players sometimes have to wait a while before a vehicle spawns in. Is there a way to make the vehicles load in the loading screen so that they spawn instantly?

Thanks in advance,
Daniel

It might help if you show the script that is used to spawn the vehicles. It might just need some optimization.

You could also loop through a table of your models when player is first active in session and request models at that point but I’m unsure if that would solve your problem completely.

1 Like

Hi Anders,

The main script we’re using to spawn vehicles is the one from Mellotrainer:

local function SpawnVehicle(model, x, y, z, heading, ped)
	-- Just in case they are in a vehicle which this trainer didn't spawn.
	if not lastVehicle and GetVehiclePedIsIn(ped, false) then
		lastVehicle = GetVehiclePedIsIn(ped, false)
	end

	if IsModelValid(model) then
		_LoadModel( model )

		local veh = CreateVehicle( model, x, y, z + 1, heading, true, true )

		if featureSpawnInsideCar then
			SetPedIntoVehicle(ped, veh, -1)
		end

		if featureDeleteLastVehicle then
			_SetEntityAsNoLongerNeeded(veh)
			-- Remove the last vehicle.
			if (lastVehicle) then
				if(GetVehicleNumberOfPassengers(lastVehicle) ~= 0 or IsVehicleSeatFree(lastVehicle, -1) == false) then
					drawNotification("~r~Last Vehicle could not be deleted.")
				else
					SetEntityAsMissionEntity(lastVehicle, true, true)
					DeleteVehicle(lastVehicle)
				end
			end
		end

		drawNotification("~g~Vehicle spawned!")
		lastVehicle = veh
		UpdateVehicleFeatureVariables( veh )
		toggleRadio(ped)

		SetModelAsNoLongerNeeded( veh )

		return veh 
	else
		drawNotification("~r~Invalid Model!")
	end
end
1 Like

Is it only addon/replace vehicles that are taking the surplus time to spawn or is it all vehicles?

If it is just the custom vehicles and you have a table already that has all of these model names or hashes in it you can try the other approach I mentioned, but no guarantee it would work. It would look something like this in a client script.

local custommodels = 
{
	"MODELNAME1",
	"MODELNAME2"
} -- Assuming this table is full of model names that will return a hash with GetHashKey(custommodels[0]), etc.

Citizen.CreateThread(function()
	while not NetworkIsPlayerActive(PlayerId()) do
		Citizen.Wait(0)
	end
	
	for i = 1, #custommodels, 1 do
		RequestModel(GetHashKey(i))  --If your table already contains hashes remove GetHashKey and just RequestModel(i)
	end
	Citizen.Wait(0)
end)

If this is happening with vanilla vehicles you very likely need to start looking at removing or optimizing other resources that are bogging down performance on your clients overall.

People are probably downloading / caching them, thats why they are not appearing, downloading them make take longer if you say have a full server all people in custom cars…

How many addon / replacer cars do you have?

Also to my knowledge its’s only when the cars are spawned in is when the (down)load. Also having heaps of cars downloading in the loading screen would make loading times way longer, say if it was downloading 100 vehicles before entering, that would take a lonnnngggg time for people with not so good internet, which is lots of people :frowning:

Thanks for the quick response. It’s just the custom / added cars. I’ll definitely try this out.

Hah, you’ve got a point there. I’ve got about 30/40 custom vehicles in my server.
I know it’s the caching and downloading that causes the delay, that’s why I wanted the users to download them in the loading screen.

I’ll try @anders’ solution and I’ll let you know.

1 Like

@dmvdr

Did you ever get a fix for this? I’m trying to find a way to have my cars pre-loaded in loading screen as well, espcially as cars seem to cause hampers.

1 Like

You can technically RequestModel/wait for each model to be loaded with some custom UI before spawning a player, but this is kind of bad for the user experience… there’s probably way better ways to integrate loading time into your spawning resource/use smaller vehicle models.

That would be great… We currently have 60-70 custom cars… A little too many for my taste, but oh well. Any idea how we could make the models loaded faster or in loading screen? Would appriciate some help! :slight_smile:

Because other development issues had to take priority, I have not had the chance to implement the code into my server. Therefore I can’t answer your question. @ItzDusty

I agree with @hydrogen, it’d be amazing if we could implement the loading into the loading screen.

1 Like

Thank you for the answer! Maybe @hydrogen can help us out with the code o.o ?

Oh, also, in this example you should definitely use a wait-if-loaded loop + SetModelAsNoLongerNeeded right after RequestModel if you don’t want people to run out of memory and end up unable to load the world.

this is useful, because many of the servers are having issues with the people that are having bad internet, it takes them soo long so they can load a vehicle. I would definitely like to see this working.

Just curious but I too am having this issue. I only have 4-5 ELS enabled law enforcement vehicles on my server and when people teleport to me they say the car isn’t loaded yet and they have to wait around 5-10 seconds for it to load. Any possibility on fixing this other than making people preload the models?

(these are not addon vehicles these are replace vehicles btw)

This isn’t an issue. There is no way to get around the client having to download the models to be able to see them.

Yes I know that, but my question was more directed towards is there a way for them to load the cars perhaps on spawn or something - because again if a player in my server teleports to me they can’t see the car until like 5-10 seconds after I’ve spawned it. I’m just a bit anxious on how it’s going to affect rolepaly in my server because if someone new joins, grabs a car and whatever and they can’t see the sheriff’s vehicle trying to pull them over.

I’ve put together a little something. It works for me, only it causes a little lag when the player spawns in.
Any feedback on the code is much appreciate, I’m not that great with lua.

1 Like

a) Does this work with addon vehicles.

Could you take a look at this as it crashes every time i load in.

--[[
Script made by Daniel R. (DMVDR on fivem forums)
Feel free to change and modify where you see fit.
Please do not reupload without asking me first.

To add your vehicles, add them in the vehicles array.
Don't forget to add a comma (,) to every entry but the last one.
--]]
local isVehiclesLoaded = false

local vehicles = {
"a310",
"a319",
"a320",
"a321neo",
"a333",
"a343",
"a340",
"a350",
"a380",
"atr",
"avro",
"b17",
"b707",
"b377guppy",
"737200",
"739",
"748",
"sca",
"lcf",
"sofia",
"747sp",
"b757",
"767300",
"773er",
"788",
"8q400",
"harrier2",
"ghawar",
"snowbird",
"sf50vision",
"catalina",
"208",
"bac",
"p40e",
"rafale",
"falcon7x",
"tupi",
"emb100",
"emb120",
"e145",
"e145rx",
"emb175",
"e190",
"emb195",
"emb390fedex",
"e1000",
"mq9",
"me262",
"bf109e3",
"ha420",
"ho229",
"il76",
"il96",
"l1011",
"blackbird",
"f117",
"f22a",
"u2r",
"u2s",
"p3",
"wp3",
"yf22",
"dc915",
"dc10",
"dc10f",
"md80",
"a10a",
"mig31",
"p51d",
"kate",
"xb70",
"spirit",
"e2d",
"f6f5",
"kj2000",
"spitfire",
"saab2000",
"su25ff",
"flankerf",
"tu95",
"tu22m3",
"e4b",
"afo",
"aft",
"kc767",
"kc10",
"kc130j",
"f16demo",
"f16tb",
"f4blu",
"uh1mash",
"uh1nasa",
"whitehawk",
"sh3",
"mhx",
"haitun",
"airportftruck",
"tfft",
"ambulance",
"sheriff2",
"police3",
"atcheli"
}

Citizen.CreateThread(function()
  while true do
		Citizen.Wait(0)
			if not isVehiclesLoaded then
			for i, name in ipairs(vehicles) do
				targetVehicle = name

				if not IsModelInCdimage(targetVehicle) or not IsModelAVehicle(targetVehicle) then
					Citizen.Trace(targetVehicle .. " could not be found as a vehicle.")
					return
				end

				RequestModel(targetVehicle)
				

				while not HasModelLoaded(targetVehicle) do
                    Citizen.Trace("[VehicleLoader] Loading vehicle " ..targetVehicle)
					Citizen.Wait(500) 
				end

                if HasModelLoaded(targetVehicle) then
 
                end
				SetModelAsNoLongerNeeded(targetVehicle)
			end
			isVehiclesLoaded = true
		end

  end
end)

Got any specific crash ID? It’s likely you’ve got some broken asset lying around, then.