Lớp công cụ
VoiceChatService
*Nội dung này được dịch bằng AI (Beta) và có thể có lỗi. Để xem trang này bằng tiếng Anh, hãy nhấp vào đây.
Tóm Tắt
Thuộc Tính
Phương Pháp
GetChatGroupsAsync(players: Instances):{any} |
IsVoiceEnabledForUserIdAsync(userId: User):boolean |
Tài Liệu Tham Khảo API
Thuộc Tính
DefaultDistanceAttenuation
Phương Pháp
GetChatGroupsAsync
Tham Số
players:Instances |
Lợi Nhuận
Mẫu mã
ĐiềuChuyểnNgườiChơiĐếnMáyChủTròChuyệnTốtNhất
-- Kịch bản máy chủ
local VoiceChatService = game:GetService("VoiceChatService")
local TeleportService = game:GetService("TeleportService")
local Players = game:GetService("Players")
local TARGET_PLACE_ID = 1234567890 -- ID của nơi đến
--[[
Trạng thái máy chủ toàn vũ trụ đã bị stub.
Trong thực tế, dữ liệu này có thể được lưu trữ và cập nhật bằng cách sử dụng MemoryStore
hoặc một hệ thống backend khác.
Hình dạng ví dụ:
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("Không thể lấy nhóm trò chuyện cho người chơi:", player.UserId)
return
end
-- Thu thập tất cả các ID nhóm trò chuyện mà người chơi đủ điều kiện
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
-- Chọn máy chủ có số lượng người dùng tương thích cao nhất
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("Không tìm thấy máy chủ phù hợp cho người chơi:", player.UserId)
return
end
local ok, err = pcall(function()
TeleportService:TeleportToPrivateServer(
TARGET_PLACE_ID,
bestServer.accessCode,
{ player }
)
end)
if not ok then
warn("Điều chuyển không thành công:", err)
end
end
Players.PlayerAdded:Connect(teleportPlayerToBestServer)