概要
屬性
方法
GetChatGroupsAsync(players: Instances):{any} |
IsVoiceEnabledForUserIdAsync(userId: User):boolean |
API 參考
屬性
DefaultDistanceAttenuation
方法
GetChatGroupsAsync
參數
players:Instances |
返回
範例程式碼
傳送玩家到最佳聊天伺服器
-- 伺服器腳本
local VoiceChatService = game:GetService("VoiceChatService")
local TeleportService = game:GetService("TeleportService")
local Players = game:GetService("Players")
local TARGET_PLACE_ID = 1234567890 -- 目標地點 ID
--[[
存根的宇宙範圍伺服器狀態。
實際上,這些資料可以使用 MemoryStore
或其他後端系統來儲存和更新。
範例形狀:
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("無法檢索玩家的聊天群組:", player.UserId)
return
end
-- 收集玩家有資格的所有聊天群組 ID
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
-- 選擇擁有最多相容使用者的伺服器
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("找不到適合的伺服器給玩家:", player.UserId)
return
end
local ok, err = pcall(function()
TeleportService:TeleportToPrivateServer(
TARGET_PLACE_ID,
bestServer.accessCode,
{ player }
)
end)
if not ok then
warn("傳送失敗:", err)
end
end
Players.PlayerAdded:Connect(teleportPlayerToBestServer)