====== How to teleport players properly =====
=== A short tutorial ===
Let's say that you want in your game to teleport ALL players using scripts this may seem hard for newcomers, but in reality is quite easy, for this we will use a numeric for ((More info here [[https://www.lua.org/pil/4.3.4.html| Lua numeric for page]]))
first we start by creating a function that will be the one that teleports all players, it should look like this
function TeleportAllPlayers()
end
We then add to the inside of the function the **World:getAllPlayers()** method, it takes one parameter:
* -1 (default, means all players)
* 0 (means all players that are alive)
* 1 (means all players that are dead)
and returns:
* [[developer_center:developer_editor:script:miniapi_tutorialerrorhandling|ErrorCode]]
* The number of players
* The array (list) of all players
our code should look like this:
function TeleportAllPlayers()
local result,num,array=World:getAllPlayers(-1)
end
we then add a for loop inside the function that takes i=1 and the length of the table
code should look like this
function TeleportAllPlayers()
local result,num,array=World:getAllPlayers(-1)
for i=1, #playerArray do
end
end
if you don't know what the # symbol is, it indicates the length of that table of that string, i also explain this in depth in [[developer_center:developer_editor:script:miniapi_tutorialtriggervar| How to get trigger variables in scripts]]
After that we then use the **Player:setPosition()** method, which takes the **objid** and the **x**,**y** and **z**
We know the objid is the id of the player that we want to teleport, so we can use playerArray[i] to get the current player, and using that we will teleport all players, we also modify our function so it takes the **x**,**y** and **z**
code should look like this:
function TeleportAllPlayers(x,y,z)
local result,num,array=World:getAllPlayers(-1)
for i=1, #playerArray do
Player:setPosition(playerArray[i],x,y,z)
end
end
And with that we have a function that when called, it teleports all players to the x,y and z we gave it