Skip to main content

Vehicle::destroy

Server-side function

JavaScript​

This method will destroy (delete) a vehicle completely.

Syntax​

vehicle.destroy()

Example​

In this example we add two commands, one to spawn a vehicle and one to destroy them.

Server-Side

//Create a command that creates a vehicle.
mp.events.addCommand("vehicle", (player, fulltext, model) => {
if (!fulltext.length || !model.length) return player.outputChatBox("Usage: /vehicle [model]")
const { position, heading, dimension } = player;
const vehicle = mp.vehicles.new(mp.joaat(model), position, {
heading,
numberPlate: "CAR",
locked: false,
engine: false,
dimension,
})
player.outputChatBox(`You have successfully spawned a ${model} as id: ${vehicle.id}`);
})

//Create a command that destroys a vehicle by id.
mp.events.addCommand("destroyvehicle", (player, fulltext, vid) => {
if (!fulltext.length || !vid.length) return player.outputChatBox("Usage: /destroyvehicle [id]")

const vehicleid = parseInt(vid);
if (isNaN(vehicleid)) return player.outputChatBox("Usage: /destroyvehicle [id]");

const vehicle = mp.vehicles.at(vehicleid);
if (!vehicle || !mp.vehicles.exists(vehicle)) {
return player.outputChatBox("We couldn't find any vehicle with that id.");
}
player.outputChatBox(`You have successfully deleted vehicle id ${vehicleid}`);
})