Text Chat Service
概要
屬性
方法
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} |
活動
BubbleDisplayed(partOrCharacter: Instance,textChatMessage: TextChatMessage):RBXScriptSignal |
MessageReceived(textChatMessage: TextChatMessage):RBXScriptSignal |
SendingMessage(textChatMessage: TextChatMessage):RBXScriptSignal |
回調
OnBubbleAdded(message: TextChatMessage,adornee: Instance):Tuple |
OnChatWindowAdded(message: TextChatMessage):Tuple |
OnIncomingMessage(message: TextChatMessage):Tuple |
API 參考
屬性
Chat Version
方法
Can Users Chat Async
Can Users Direct Chat Async
返回
範例程式碼
CanUsersDirectChatAsync
local TextChatService = game:GetService("TextChatService")
-- 替換為兩個用戶的 UserIds
local userId1 = 1
local userId2 = 2
local directChatParticipants = TextChatService:CanUsersDirectChatAsync(userId1, { userId2 })
-- 檢查符合條件的參與者
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("無法創建 TextChannel。符合條件的用戶不足。")
return nilDisplay Bubble
Get Chat Groups Async
參數
players:Instances |
返回
範例程式碼
傳送玩家到最佳聊天伺服器
-- 伺服器腳本
local TextChatService = game:GetService("TextChatService")
local TeleportService = game:GetService("TeleportService")
local Players = game:GetService("Players")
local TARGET_PLACE_ID = 1234567890 -- 目的地地方 ID
--[[
全域伺服器狀態的佔位符。
在實際情況中,這些數據可以使用 MemoryStore
或其他後端系統進行存儲和更新。
示例形狀:
ActiveServers = {
{
serverId = "abc",
privateServerId = game.PrivateServerId,
groupUserCounts = {
groupA = 12,
groupB = 5
}
},
{
serverId = "def",
privateServerId = game.PrivateServerId,
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("無法檢索玩家的聊天群組:", 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()
local teleportOptions = Instance.new("TeleportOptions")
teleportOptions.ReservedServerId = bestServer.privateServerId
TeleportService:TeleportAsync(
TARGET_PLACE_ID,
{ player },
teleportOptions
)
end)
if not ok then
warn("傳送失敗:", err)
end
end
Players.PlayerAdded:Connect(teleportPlayerToBestServer)獲取聊天群組異步
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("無法獲取聊天群組")
return
end
for _, group in ipairs(chatGroups) do
-- 每個群組包含聊天群組 ID 字串。
-- 共享匹配的群組 ID 的玩家可以互相聊天。
for _, groupId in ipairs(group) do
print("聊天群組 ID:", groupId)
end
end活動
Bubble Displayed
Text Chat Service. Bubble Displayed(
參數
Message Received
參數
Sending Message
參數
回調
On Bubble Added
參數
返回
On Chat Window Added
On Incoming Message