getElementByIndex | Multi Theft Auto: Wiki Skip to content

getElementByIndex

Client-side
Server-side
Shared

This function returns an element of the specified type with the specified index.

Tip

If you wish to retrieve more than one element at a time, it is more efficient to use getElementsByType instead.

OOP Syntax Help! I don't understand this!

Syntax

element|false getElementByIndex ( ​string theType, [ ​int index = 0 ] )
Required arguments
  • theType: The type of the element to be returned. See element types.
Optional arguments

Note: when using optional arguments, you might need to supply all arguments before the one you wish to use.

  • index (default: 0): The element's index (0 for the first element, 1 for the second, etc).

Returns

Returns the requested element, or false if it doesn't exist.

  • element|false: result

Code Examples

client

This example generates the name of the specified player that is on the server. For example player 0 would return the name of the first player on the server.

function showPlayer(command, index)
local thePlayer = getElementByIndex("player", tonumber(index))
if thePlayer then
outputChatBox("Player " .. index .. " is a: " ..getPlayerName(thePlayer))
else
outputChatBox("Player not found.")
end
end
addCommandHandler("player", showPlayer)

See Also

Element Functions