Jump to content
RAGE Multiplayer Community

How can I destroy all vehicles binded to a player?


Recommended Posts

Hello. I'm trying to destroy all the vehicles binded to a player once he quits the server. But there's an error... Why is this:
 

mp.events.add("playerQuit", (player) => {
    var arrayLength = player.personalVehicles.length;
    for (var i = 0; i < arrayLength; i++) {
        console.log(player.personalVehicles[i]);
        vehicle = player.personalVehicles[i];
        vehicle.destroy();
    }
});


Returning this error:

C:\RAGEMP\server-files\packages\RPG\vehicles.js:52: TypeError: vehicle.destroy is not a function
        vehicle.destroy();

 

The `console.log(player.personalVehicles);` returns:
 

{
  vehicle: {
    setVariable: [Function (anonymous)],
    setVariables: [Function (anonymous)]
  }
}

The vehicles are spawned like this:

mp.events.add("playerReady", (player) => {
    pool.getConnection((err, con) => {
        if (err) console.log(err)
        console.log()
            con.query("SELECT vehicleID, vehicleModel, vehicleSpawnX, vehicleSpawnY, vehicleSpawnZ FROM vehicles WHERE vehicleType = ? AND vehicleOwner = ?", ["players", player.playerID], function (err, result) {
                if (err) console.log(err)
                player.personalVehicles = []
                for (i = 0; i < result.length; i++) {
                    vehicleModel = result[i].vehicleModel
                    player.personalVehicles.push({vehicle: mp.vehicles.new(parseInt(vehicleModel), new mp.Vector3(result[i].vehicleSpawnX, result[i].vehicleSpawnY, result[i].vehicleSpawnZ))})
                }
            });
        con.release()
    });
});

 

Link to post
Share on other sites
  • 3 weeks later...

Hello,
 

at your creation on 

player.personalVehicles.push(

do like this (without the extra vehicle variable):

const vehicle = mp.vehicles.new(parseInt(vehicleModel), new mp.Vector3(result[i].vehicleSpawnX, result[i].vehicleSpawnY, result[i].vehicleSpawnZ));

player.personalVehicles.push(vehicle);

and look what 

console.log(player.personalVehicles[i]);

says now.

 

and you should check if the objects are really existent:

mp.events.add("playerQuit", (player) => {
	console.log(player.personalVehicles); // see if anything are correct in this array
	for (var i = 0; i<player.personalVehicles.length; i++) {
		console.log(player.personalVehicles[i]); 
		if(player.personalVehicles[i]) vehicle.destroy();
	}
});
                                                    
                                                    

 

 

Tipp: with typescript and typeorm you are faster and better to write codes.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...