Engine Class
VoiceChatService
Summary
Properties
Methods
GetChatGroupsAsync(players: Instances):{any} |
IsVoiceEnabledForUserIdAsync(userId: User):boolean |
API Reference
Properties
DefaultDistanceAttenuation
Methods
GetChatGroupsAsync
Parameters
players:Instances |
Returns
Code Samples
TeleportPlayerToBestChatServer
-- Server Script
local VoiceChatService = game:GetService("VoiceChatService")
local TeleportService = game:GetService("TeleportService")
local Players = game:GetService("Players")
local TARGET_PLACE_ID = 1234567890 -- Destination place ID
--[[
Stubbed universe-wide server state.
In practice, this data could be stored and updated using MemoryStore
or another backend system.
Example shape:
ActiveServers = {
{
serverId = "abc",
accessCode = "reserved-server-code",
groupUserCounts = {
groupA = 12,
groupB = 5
}
},
{
serverId = "def",
accessCode = "reserved-server-code",
groupUserCounts = {
groupB = 8
}
}
}
]]
local ActiveServers = {}
local function teleportPlayerToBestServer(player)
local success, chatGroups = pcall(function()
return VoiceChatService:GetChatGroupsAsync({ player })
end)
if not success or not chatGroups then
warn("Failed to retrieve chat groups for player:", player.UserId)
return
end
-- Collect all chat group IDs the player is eligible for
local playerGroupIds = {}
for _, group in ipairs(chatGroups) do
for _, groupId in ipairs(group) do
playerGroupIds[groupId] = true
end
end
local bestServer = nil
local bestUserCount = 0
-- Choose the server with the highest number of compatible users
for _, server in ipairs(ActiveServers) do
local compatibleUserCount = 0
for groupId, userCount in pairs(server.groupUserCounts) do
if playerGroupIds[groupId] then
compatibleUserCount += userCount
end
end
if compatibleUserCount > bestUserCount then
bestUserCount = compatibleUserCount
bestServer = server
end
end
if not bestServer then
warn("No suitable server found for player:", player.UserId)
return
end
local ok, err = pcall(function()
TeleportService:TeleportToPrivateServer(
TARGET_PLACE_ID,
bestServer.accessCode,
{ player }
)
end)
if not ok then
warn("Teleport failed:", err)
end
end
Players.PlayerAdded:Connect(teleportPlayerToBestServer)