The game sometimes does not spawn vehicles

Greetings!

I have a trouble with my Custom resource, i need spawn a car on the other side of the map, but 50-50% to succeed. I don’t know why, I have read many descriptions on the subject.

The Important part of the code:

var vh = "adder";
RequestModel( vh );
spawnCar(vh, x, y, z, a,  true, false);

let tryes = 0;

function spawnCar(car, x,y,z, a, b1, b2){
	
	tryes ++;
	
	if(HasModelLoaded(car)){
		auto = CreateVehicle(car, x,y,(z+0.15), a, b1, b2)
		// success 
	} else {
		if(tryes > 5){
			//fail
		} else {
			setTimeout(function(){
				spawnCar(car, x,y,z, a, b1, b2);
			}, 200);
		}
	}
}

Thanks for the help!

You dont seem to be requesting the model first - you need to do that before checking for it loading

As you can see, I did so the first time I called the function

RequestModel( vh );

And only then did I try to spawn the vehicle

i need to spawn a car on the other side of the map

You might be facing issues because the “other side of the map” might not be loaded for the player. This causes the vehicle to spawn then endlessly fall through the map because it doesn’t have anything to collide with.

A solution could be to use this native: SET_ENTITY_LOAD_COLLISION_FLAG - Cfx.re Docs

Otherwise, perhaps try to spawn the vehicle only when the player is within range of that “side of the map”, perhaps a 250.0 or 500.0 radius would do the trick.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.