学习
引擎类
TextChatService
无法创建
服务

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处



API 参考
属性
ChatTranslationEnabled
未复制
Roblox 脚本安全性
读取并联
功能: Chat
TextChatService.ChatTranslationEnabled:boolean

ChatVersion
已弃用

CreateDefaultCommands
插件安全性
读取并联
功能: Chat
TextChatService.CreateDefaultCommands:boolean

CreateDefaultTextChannels
插件安全性
读取并联
功能: Chat
TextChatService.CreateDefaultTextChannels:boolean

方法
CanUserChatAsync
暂停
功能: Chat
TextChatService:CanUserChatAsync(userId:User):boolean
参数
userId:User
返回

CanUsersChatAsync
暂停
功能: Chat
TextChatService:CanUsersChatAsync(
userIdFrom:User, userIdTo:User
参数
userIdFrom:User
userIdTo:User
返回

CanUsersDirectChatAsync
暂停
功能: Chat
TextChatService:CanUsersDirectChatAsync(
requesterUserId:User, userIds:{any}
参数
requesterUserId:User
userIds:{any}
返回
代码示例
用户是否可以直接聊天异步
local TextChatService = game:GetService("TextChatService")
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 nil

DisplayBubble
功能: Chat
TextChatService:DisplayBubble(
partOrCharacter:Instance, message:string
):()
参数
partOrCharacter:Instance
message:string
返回
()

GetChatGroupsAsync
暂停
功能: Chat
TextChatService:GetChatGroupsAsync(players:Instances):{any}
参数
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",
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("无法为玩家获取聊天组:", 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)

活动
BubbleDisplayed
功能: Chat
TextChatService.BubbleDisplayed(
partOrCharacter:Instance, textChatMessage:TextChatMessage
参数
partOrCharacter:Instance
textChatMessage:TextChatMessage

MessageReceived
功能: Chat
TextChatService.MessageReceived(textChatMessage:TextChatMessage):RBXScriptSignal
参数
textChatMessage:TextChatMessage

SendingMessage
功能: Chat
TextChatService.SendingMessage(textChatMessage:TextChatMessage):RBXScriptSignal
参数
textChatMessage:TextChatMessage

回调
OnBubbleAdded
功能: Chat
TextChatService.OnBubbleAdded(
message:TextChatMessage, adornee:Instance
参数
adornee:Instance
返回

OnChatWindowAdded
功能: Chat
TextChatService.OnChatWindowAdded(message:TextChatMessage):Tuple
参数
返回

OnIncomingMessage
功能: Chat
TextChatService.OnIncomingMessage(message:TextChatMessage):Tuple
参数
返回

©2026 Roblox Corporation、Roblox、Roblox 标志及 Powering Imagination 是我们在美国及其他国家或地区的注册与未注册商标。