Virtual World System

Overview

The Virtual World system provides a comprehensive solution for managing isolated player environments within your server. Players in different virtual worlds cannot interact with each other, making it perfect for creating private sessions, instances, or separate game areas.

Key Concepts

  • Global World (ID: 0): The default world where all players start

  • Virtual World: An isolated environment with its own set of players

  • Routing Bucket: The underlying mechanism that separates players between worlds

API Reference

VirtualWorld:Create(players, virtualWorldId)

Creates a new virtual world and moves the specified players into it.

Parameters

  • players (table): Array of player IDs to add to the virtual world

  • virtualWorldId (number, optional): Specific ID for the virtual world. If not provided, auto-generates the next available ID

Returns

  • virtualWorld (table): The created virtual world object containing id and players

Example

Notes

  • If a player is already in another virtual world, they will be automatically removed from their current world

  • Players are moved using SetPlayerRoutingBucket() function


VirtualWorld:IsValidWorld(virtualWorldId)

Checks if a virtual world exists.

Parameters

  • virtualWorldId (number): The virtual world ID to validate

Returns

  • boolean: True if the world exists, false otherwise

Example


VirtualWorld:DeleteWorld(virtualWorldId)

Deletes a virtual world and moves all its players back to the global world.

Parameters

  • virtualWorldId (number): The virtual world ID to delete

Returns

  • None

Example

Notes

  • All players in the deleted world are automatically moved to the global world (ID: 0)

  • If the world doesn't exist, the function returns silently


VirtualWorld:AddPlayerToGlobalWorld(playerId)

Moves a player to the global world (ID: 0).

Parameters

  • playerId (number): The player ID to move to the global world

Returns

  • None

Example


VirtualWorld:AddPlayerOnVirtualWorld(playerId, virtualWorldId)

Adds a player to a specific virtual world. If the world doesn't exist, it creates a new one.

Parameters

  • playerId (number): The player ID to add

  • virtualWorldId (number): The target virtual world ID

Returns

  • virtualWorld (table): The virtual world object (if created new)

  • None (if added to existing world)

Example

Events Triggered

  • VirtualWorld:PlayerAddedToWorld: Fired when a player is successfully added to a world

Notes

  • If the player is already in another world, they are automatically removed first

  • If the target world doesn't exist, it creates a new world with the player


VirtualWorld:RemovePlayerFromVirtualWorld(playerId)

Removes a player from their current virtual world.

Parameters

  • playerId (number): The player ID to remove

Returns

  • None

Example

Events Triggered

  • VirtualWorld:PlayerLeftFromWorld: Fired when a player is successfully removed from a world

Notes

  • If the player is not in any virtual world, the function returns silently

  • The player's routing bucket is reset and they're removed from tracking


VirtualWorld:GetPlayerVirtualWorld(playerId)

Gets the virtual world object that a player is currently in.

Parameters

  • playerId (number): The player ID to query

Returns

  • virtualWorld (table): The virtual world object containing id and players

  • nil: If the player is not in any virtual world

Example


VirtualWorld:GetPlayersFromVirtualWorld(virtualWorldId)

Gets all players currently in a specific virtual world.

Parameters

  • virtualWorldId (number): The virtual world ID to query

Returns

  • players (table): Array of player IDs in the virtual world

  • {} (empty table): If the world doesn't exist

Example

Events

The system triggers the following events that you can listen to:

VirtualWorld:PlayerAddedToWorld

Fired when a player is added to a virtual world.

Parameters:

  • playerId (number): The player who was added

  • virtualWorldId (number): The world they were added to

VirtualWorld:PlayerLeftFromWorld

Fired when a player leaves a virtual world.

Parameters:

  • playerId (number): The player who left

  • virtualWorldId (number): The world they left from

Usage Examples

Creating a Private Session

Managing Player Movements

Event Handling

World Management

Best Practices

  1. Always validate worlds before performing operations:

  2. Handle player disconnections by removing them from virtual worlds:

  3. Use meaningful world IDs for better organization:

  4. Monitor world populations to manage server resources:

Troubleshooting

Common Issues

Players can still see each other after being separated

  • Ensure SetPlayerRoutingBucket() is properly implemented in your server

  • Check that the virtual world IDs are different

Memory leaks from abandoned worlds

  • Implement regular cleanup of empty virtual worlds

  • Always delete worlds when they're no longer needed

Players stuck in virtual worlds

  • Use VirtualWorld:AddPlayerToGlobalWorld() as a fallback

  • Implement admin commands to force player movement

Debug Information

Last updated