Player Index
When a player joins your world instance, they’re assigned a constant index. This index persists while they are in the instance and you can use it to track players and any objects tied to them.
This feature will help save time and improve your resource management because you can quickly iterate through the indices to retrieve information about players that join or leave your world. The index starts at zero and is incremented for each additional player until it reaches the maximum number of players allowed for the world. When players leave, their indices are released for re-use by new players.
To retrieve a Player’s index in the world, use the index.get()
function on a Player object.
Returns the player’s index within the world instance.
Parameters
None
Return Type
Number
Getting a Player from an Index
To retrieve a Player object based on a Player Index value, use the getPlayerFromIndex()
function on the this.world
object.
World.getPlayerFromIndex(index: number)
Returns a Player object based on the provided index. Returns null if no Player is associated with the index, such as when a player has left the world and has not been replaced by a new player.
Parameters
A number representing a Player Index in the world instance.
Return Type
Either a Player or null.
var playerIndex = player.index.get();
var playerFromIndex = this.world.getPlayerFromIndex(playerIndex);
When developing a world, you might want to perform special actions when a player is in Build Mode as opposed to Preview Mode. You can check if a player is in Build Mode by using the player function isInBuildMode.get()
.
Player.isInBuildMode.get()
Indicates whether the player is in Build Mode. true
if the player is in build mode; otherwise, false
.
Parameters
None
Return type
Boolean
isInBuildMode = player.isInBuildMode.get();