Add a Vehicle

Revision as of 02:22, 9 April 2025 by Stepbro (talk | contribs) (Removed 'addToWorld()' function invoke as the webpage for 'addToWorld()' (https://wiki.gtaconnected.com/addToWorld) states that it no longer needs to be invoked.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Description

This code adds a vehicle to the game when a player types /vehicle model in-game.
model must be an integer representing a vehicle model ID, this command example does not work with vehicle model names.
Validation included in this code: Check that the player is spawned, and check that the player typed at least some text after the command name.
Validation not included in this code: Check that the vehicle model ID is valid.

Code

JavaScript, Server-Side addCommandHandler('vehicle', function(command, parameters, client) { if(!client.player) { messageClient('You must be spawned to spawn a vehicle!', client); } else if(parameters.trim() == ) { messageClient('You must type a model number! Command syntax: /vehicle model', client); } else { var model = parseInt(parameters); var vehicle = gta.createVehicle(model, client.player.position); } });