Lớp công cụ
TextChatService
*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
CanUserChatAsync(userId: User):boolean |
CanUsersChatAsync(userIdFrom: User,userIdTo: User):boolean |
CanUsersDirectChatAsync(requesterUserId: User,userIds: {any}):{any} |
DisplayBubble(partOrCharacter: Instance,message: string):() |
GetChatGroupsAsync(players: Instances):{any} |
Sự Kiện
BubbleDisplayed(partOrCharacter: Instance,textChatMessage: TextChatMessage):RBXScriptSignal |
MessageReceived(textChatMessage: TextChatMessage):RBXScriptSignal |
SendingMessage(textChatMessage: TextChatMessage):RBXScriptSignal |
Gọi Lại
OnBubbleAdded(message: TextChatMessage,adornee: Instance):Tuple |
OnChatWindowAdded(message: TextChatMessage):Tuple |
OnIncomingMessage(message: TextChatMessage):Tuple |
Tài Liệu Tham Khảo API
Thuộc Tính
ChatVersion
Phương Pháp
CanUserChatAsync
CanUsersChatAsync
CanUsersDirectChatAsync
Lợi Nhuận
Mẫu mã
CanUsersDirectChatAsync
local TextChatService = game:GetService("TextChatService")
local directChatParticipants = TextChatService:CanUsersDirectChatAsync(userId1, { userId2 })
-- Kiểm tra các người tham gia đủ điều kiện
if #directChatParticipants > 0 then
local directChannel = Instance.new("TextChannel")
directChannel.Parent = TextChatService
for _, participant in directChatParticipants do
directChannel:AddUserAsync(participant)
end
return directChannel
end
warn("Không thể tạo TextChannel. Không đủ người dùng đủ điều kiện.")
return nilDisplayBubble
GetChatGroupsAsync
Tham Số
players:Instances |
Lợi Nhuận
Mẫu mã
Di Chuyển Người Chơi Đến Máy Chủ Trò Chuyện Tốt Nhất
-- Kịch bản máy chủ
local TextChatService = game:GetService("TextChatService")
local TeleportService = game:GetService("TeleportService")
local Players = game:GetService("Players")
local TARGET_PLACE_ID = 1234567890 -- ID địa điểm đích
--[[
Trạng thái máy chủ toàn vũ trụ đã được stub.
Trên 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 TextChatService: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ả 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("Di chuyển thất bại:", err)
end
end
Players.PlayerAdded:Connect(teleportPlayerToBestServer)Lấy Nhóm Trò Chuyện Bất Đồng Bộ
local TextChatService = game:GetService("TextChatService")
local Players = game:GetService("Players")
local players = Players:GetPlayers()
local success, chatGroups = pcall(function()
return TextChatService:GetChatGroupsAsync(players)
end)
if not success then
warn("Không thể lấy nhóm trò chuyện")
return
end
for _, group in ipairs(chatGroups) do
-- Mỗi nhóm chứa các chuỗi ID nhóm trò chuyện.
-- Người chơi có cùng ID nhóm có thể trò chuyện với nhau.
for _, groupId in ipairs(group) do
print("ID nhóm trò chuyện:", groupId)
end
endSự Kiện
BubbleDisplayed
TextChatService.BubbleDisplayed(
Tham Số
MessageReceived
Tham Số
SendingMessage
Tham Số
Gọi Lại
OnBubbleAdded
Tham Số
Lợi Nhuận
OnChatWindowAdded
Tham Số
Lợi Nhuận
OnIncomingMessage
Tham Số
Lợi Nhuận