Engine Class
VoiceChatService
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
สรุป
คุณสมบัติ
วิธีการ
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 สถานที่ปลายทาง
--[[
สถานะเซิร์ฟเวอร์ทั่วจักรวาลที่ถูก stub.
ในทางปฏิบัติ ข้อมูลนี้สามารถถูกเก็บและอัปเดตโดยใช้ 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
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
การเปิดใช้งานเสียงสำหรับรหัสผู้ใช้แบบอะซิงโครนัส
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