Vehicle::destroy: Difference between revisions

From RAGE Multiplayer Wiki
No edit summary
m (Fixed numerous "outputChatBox" typographical errors that would break the code example.)
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{ServersideJsFunction}}
{{JSContainer|
This method will destroy (delete) a vehicle completely.
== Syntax ==
<syntaxhighlight lang="javascript">
<syntaxhighlight lang="javascript">
mp.events.add('playerCommand', (player, cmd) => {
vehicle.destroy()
let arr = cmd.split(' ');
</syntaxhighlight>
if (arr[0] == 'destroyrandomveh') {
 
let vehicles = mp.vehicles.toArray();
== Example ==
if (vehicles.length > 0) {
In this example we add two commands, one to spawn a vehicle and one to destroy them.
let randomValue = Math.floor(Math.random() * vehicles.length);
{{ServersideCode|
vehicles[randomValue].destroy();
<syntaxhighlight lang="javascript">
} else {
//Create a command that creates a vehicle.
player.outputChatBox('Vehicles does not exist!');
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}`);
})
</syntaxhighlight>
</syntaxhighlight>
}}
}}
==See Also==
{{Vehicle_definition}}

Latest revision as of 11:04, 26 July 2024

Server-Side
Function

 JavaScript



JavaScript Syntax

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}`);
})


See Also