Add Object

From GTA Connected
Revision as of 14:43, 14 August 2018 by Mex (talk | contribs) (Mex moved page ScriptingExamples/AddObject to OldScriptingExamples/AddObject)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Description

This code adds an object of the specified model index when typing /o model
The model index defaults to 583 (a ramp) if it isn't specified or it isn't a number.
A chat message is also displayed for all players when the command is used.

Code

Lua, Server-Side, GTAC versions 1.0.71 and below:

addCommandHandler("o",function(commandName,arguments,client)
	local player = client.player
	local model = arguments:len() > 0 and tonumber(arguments) or 583
	local object = game.createElement(ELEMENT_OBJECT, player.game)
	object.modelIndex = model
	object.game = player.game
	object.position = getPositionInFrontOfRad(player.position, player.heading, 8.0)
	object.heading = player.heading
	addToWorld(object)
	outputChatBox(player.name .. ' added an object with model ID ' .. model .. '.', COLOUR_GREEN)
end)

function getPositionInFrontOfRad(pos, rad, radius)
	rad = rad + math.pi / 2
	pos.x = pos.x + radius * math.cos(rad)
	pos.y = pos.y + radius * math.sin(rad)
	return pos
end