ScriptingExamples/AddObject: Difference between revisions

From GTA Connected
Jump to navigation Jump to search
No edit summary
 
(No difference)

Latest revision as of 00:27, 11 December 2019

Description

This code adds an object to the game when a player types /object model in-game.
model must be an integer representing an object model ID.
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 object model ID is valid.

Code

JavaScript, Server-Side

addCommandHandler('object', function(command, parameters, client)
{
	if(!client.player)
	{
		messageClient('You must be spawned to spawn an object!', client);
	}
	else if(parameters.trim() == '')
	{
		messageClient('You must type a model number! Command syntax: /object model', client);
	}
	else
	{
		var model = parseInt(parameters);
		var object = gta.createObject(model, client.player.position);
		addToWorld(object);
	}
});