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ã
Dịch Người Chơi Tới Máy Chủ Chat 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 của địa điểm đích
--[[
Trạng thái máy chủ vũ trụ toàn cầu đã được khởi tạo.
Trên thực tế, dữ liệu này có thể được lưu trữ và cập nhật bằ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 chat cho người chơi:", player.UserId)
return
end
-- Thu thập tất cả các ID nhóm chat 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ủ với 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("Dịch chuyển thất bại:", err)
end
end
Players.PlayerAdded:Connect(teleportPlayerToBestServer)Sự 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