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
-- Create a virtual world with specific players
local world = VirtualWorld:Create({1, 2, 3})
print("Created world with ID:", world.id)
-- Create a virtual world with a specific ID
local customWorld = VirtualWorld:Create({4, 5}, 100)
print("Created world with custom ID:", customWorld.id)
if VirtualWorld:IsValidWorld(100) then
print("World 100 exists!")
else
print("World 100 does not exist")
end
-- Delete world 100
VirtualWorld:DeleteWorld(100)
print("World 100 has been deleted")
-- Move player 5 to the global world
VirtualWorld:AddPlayerToGlobalWorld(5)
-- Add player 1 to world 50
VirtualWorld:AddPlayerOnVirtualWorld(1, 50)
-- Add player 2 to world 75 (creates world if it doesn't exist)
VirtualWorld:AddPlayerOnVirtualWorld(2, 75)
-- Remove player 3 from their current world
VirtualWorld:RemovePlayerFromVirtualWorld(3)
local playerWorld = VirtualWorld:GetPlayerVirtualWorld(1)
if playerWorld then
print("Player 1 is in world:", playerWorld.id)
print("Players in this world:", #playerWorld.players)
else
print("Player 1 is not in any virtual world")
end
local players = VirtualWorld:GetPlayersFromVirtualWorld(50)
print("Players in world 50:", #players)
for i, playerId in ipairs(players) do
print("Player", i, ":", playerId)
end
-- Create a private world for a group of friends
local friendsWorld = VirtualWorld:Create({10, 15, 22, 8})
print("Friends are now in private world:", friendsWorld.id)
-- Check if player is in a world before moving them
local currentWorld = VirtualWorld:GetPlayerVirtualWorld(5)
if currentWorld then
print("Player 5 is currently in world:", currentWorld.id)
-- Move them to a different world
VirtualWorld:AddPlayerOnVirtualWorld(5, 100)
else
print("Player 5 is in the global world")
-- Add them to a virtual world
VirtualWorld:AddPlayerOnVirtualWorld(5, 50)
end
-- Listen for players joining worlds
RegisterNetEvent("VirtualWorld:PlayerAddedToWorld")
AddEventHandler("VirtualWorld:PlayerAddedToWorld", function(playerId, worldId)
print("Player", playerId, "joined world", worldId)
-- Send welcome message or setup world-specific features
end)
-- Listen for players leaving worlds
RegisterNetEvent("VirtualWorld:PlayerLeftFromWorld")
AddEventHandler("VirtualWorld:PlayerLeftFromWorld", function(playerId, worldId)
print("Player", playerId, "left world", worldId)
-- Cleanup player-specific data
end)
-- Create multiple worlds for different activities
local racingWorld = VirtualWorld:Create({1, 2, 3, 4}, 200)
local deathMatchWorld = VirtualWorld:Create({5, 6, 7, 8}, 201)
-- Later, clean up the worlds
VirtualWorld:DeleteWorld(200) -- Racing participants return to global world
VirtualWorld:DeleteWorld(201) -- DM participants return to global world
if VirtualWorld:IsValidWorld(worldId) then
-- Safe to proceed
end
AddEventHandler("playerDropped", function()
local playerId = source
VirtualWorld:RemovePlayerFromVirtualWorld(playerId)
end)
local RACING_WORLD = 1000
local DEATHMATCH_WORLD = 2000
local MISSION_WORLD_BASE = 3000
local players = VirtualWorld:GetPlayersFromVirtualWorld(worldId)
if #players == 0 then
VirtualWorld:DeleteWorld(worldId) -- Clean up empty worlds
end
-- Get all virtual worlds (for debugging)
for worldId, world in pairs(VirtualWorlds) do
print("World", worldId, "has", #world.players, "players")
end
-- Check player's current world
local playerId = 1
local world = VirtualWorld:GetPlayerVirtualWorld(playerId)
if world then
print("Player", playerId, "is in world", world.id)
else
print("Player", playerId, "is in global world or not tracked")
end