setElementPosition
Pair: getElementPosition
This function sets the position of an element to the specified coordinates.
Do not use this function to spawn a player. It will cause problems with other functions like warpPedIntoVehicle. Use spawnPlayer instead.
If you want to put a vehicle or player out of the water or simulate the position-resetting behaviour if CJ goes below the ground too far, then you need to retrieve a recommended coordinate on ground to place the element at. Take a look at this MTA forums post for steps in the right direction.
OOP Syntax Help! I don't understand this!
- Method: element:setPosition(...)
- Variable: .position
Syntax
bool setElementPosition ( element theElement, float x, float y, float z, [ bool warp = true ] )
Required Arguments
- theElement: A valid element to set the position.
- x: The x coordinate of the destination.
- y: The y coordinate of the destination.
- z: The z coordinate of the destination.
Optional Arguments
- warp (default: true): teleports players, resetting any animations they were doing. Setting this to false preserves the current animation.
Returns
- bool: result
Returns true if the function was successful, false otherwise.
Code Examples
This example teleports the player to map center using /zero command. (aka. origin point)
function teleportPlayerToMapCenter() -- teleport player to 0,0,0 coordinates (map origin/center) setElementPosition(localPlayer, 0, 0, 0 + 3); -- add +3 to z coordinate to not fall below map!endaddCommandHandler("zero", teleportPlayerToMapCenter);
This example teleports the player to another random player using /tprandom command.
function teleportToRandomPlayer(uPlayer) -- get a random player (does not exclude the player executing this command!) local uRandomPlayer = getRandomPlayer();
-- get the position of the random player local fX, fY, fZ = getElementPosition(uRandomPlayer);
-- teleport the player to random one with slight offset to not get stuck setElementPosition(uPlayer, fX + 2, fY + 2, fZ);endaddCommandHandler("tprandom", teleportToRandomPlayer);
See Also
Element Functions
- getElementPosition
- setElementPosition
- getElementHealth
- setElementHealth