概要
屬性
方法
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)獲取聊天群組異步
local VoiceChatService = game:GetService("VoiceChatService")
local Players = game:GetService("Players")
local players = Players:GetPlayers()
local success, chatGroups = pcall(function()
return VoiceChatService:GetChatGroupsAsync(players)
end)
if not success then
warn("無法獲取聊天群組")
return
end
for _, group in ipairs(chatGroups) do
-- 每個群組包含聊天群組 ID 字串。
-- 共享相同群組 ID 的玩家可以互相聊天。
for _, groupId in ipairs(group) do
print("聊天群組 ID:", groupId)
end
endIsVoiceEnabledForUserIdAsync
參數
返回
範例程式碼
用戶 ID 的語音是否啟用(非同步)
local Players = game:GetService("Players")
local VoiceChatService = game:GetService("VoiceChatService")
local localPlayer = Players.LocalPlayer
local success, enabled = pcall(function()
return VoiceChatService:IsVoiceEnabledForUserIdAsync(localPlayer.UserId)
end)
if success and enabled then
print("語音聊天已啟用!")
end