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 worldvirtualWorldId(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 containingidandplayers
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 addvirtualWorldId(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 containingidandplayersnil: 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 addedvirtualWorldId(number): The world they were added to
VirtualWorld:PlayerLeftFromWorld
Fired when a player leaves a virtual world.
Parameters:
playerId(number): The player who leftvirtualWorldId(number): The world they left from
Usage Examples
Creating a Private Session
Managing Player Movements
Event Handling
World Management
Best Practices
Always validate worlds before performing operations:
Handle player disconnections by removing them from virtual worlds:
Use meaningful world IDs for better organization:
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 serverCheck 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 fallbackImplement admin commands to force player movement
Debug Information
Last updated