文字聊天概览

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

Roblox 提供在实时会话中玩家之间的文本消息通过 TextChatService 。该服务具有其标准功能,但还提供一组方法和事件来扩展和自定义聊天,例如基于 自定义需求 发送消息,添加特殊权限或管理到特定玩家,以及创建 自定义命令 执行特定操作。

以下部分摘要了您可以使用来自定义聊天系统的主要类别和实例。

顶层配置

单例类 TextChatService 负责管理整个聊天系统,包括处理聊天消息过滤、管理和用户权限。使用属性 like CreateDefaultTextChannelsCreateDefaultCommands 来启用或禁用默认聊天频道和命令。

默认 UI 配置

TextChatService 提供默认用户界面,可以自定义以满足您的体验需求。每一种配置都可以禁用以隐藏相关的用户界面元素,或可以替换为自定义界面如果需要。

通道、消息和命令

聊天流程图

文字聊天使用 客户端-服务器 模型,包括 发送客户端服务器接收客户端

A flowchart for in-experience text chat.
  1. 一名玩家从本地设备发送消息,触发 TextChannel:SendAsync() 方法。该方法处理消息,并确定它是聊天命令还是普通聊天消息。

  2. 服务器发射 TextChannel.ShouldDeliverCallback 来确定是否根据权限和 Roblox 社区过滤要求向其他玩家发送消息。

  3. 如果 TextChannel.ShouldDeliverCallback 确定该消息可以发送给其他玩家,服务器应用任何过滤器并触发 TextChannel.OnIncomingMessage 两次:

    1. 第一次是在发送客户端上,它表示服务器正在通过 TextChatService.MessageReceived 事件处理消息。该事件会将发送客户端上的本地消息替换为服务器处理的消息。如果原始文件没有需要过滤,那么消息也是相同的。

    2. 第二次是在接收客户端,这会触发 TextChatService.MessageReceived 事件将消息显示给其他玩家。

文本聊天钩和回调

TextChatService 鼓励在聊天消息的外观和交付上进行明确分离。文字聊天系统的多个实例提供钩和回调来在中央化、清晰的位置格式化。

A flowchart of the TextChatService callbacks order

| 回调 | 返回值 | | ----------------------------------------- | ----------------------------------- | | | boolean | | | | | | | | | | | | | | |

条件发送消息

TextChannel.ShouldDeliverCallback 应仅在服务器上定义。回调在发送消息时为文本通道的每个 TextSource 子发射,以确定消息是否应该发送。此回调可用于实现可能依赖额外游戏上下文的自定义消息传递逻辑,例如:

  • 基于距离的聊天 在这里,用户只能向靠近他们的人发送消息。
  • 防止具有某些属性的用户向其他人发送消息。

自定义消息显示

默认 TextChatService 用户界面依赖 富文本 来格式化和自定义消息显示方式。您可以使用以下回调来格式化消息,以便在显示给用户之前格式化消息,例如添加颜色或 聊天标签 到用户名或格式化消息内容。

以下回调在每次显示即将显示的 TextChatMessage 之前被调用,可以根据 TextChannelTextSourceTextChatMessage 内容自定义聊天窗口外观。当客户端发送消息时,这些回调会在消息发送到服务器时调用一次,值 TextChatMessage.Status 将为 Enum.TextChatMessageStatus.Sending 。一旦消息被服务器收到并交付给其他用户,发送客户端再次收到更新的 Enum.TextChatMessageStatus 值,将消息重新发送给其他用户。

从传统聊天迁移

本节可帮助您从遗产聊天系统迁移到使用 TextChatService 实现常见聊天功能和行为的替代方法。

  1. 资源管理器 窗口中,选择 TextChatService .

  2. 属性 窗口中,找到 ChatVersion 下拉列表并选择 TextChatService

基本功能

虽然两个系统都共享相同的基本聊天功能,但 TextChatService 实现在整体上更可持续,更容易反复使用。

功能性遗产聊天文字聊天服务差异
发送聊天消息Players:Chat()TextChannel:SendAsync()SendAsync() 方法支持更先进的聊天功能,例如丰富的文本格式和消息优先级。它还包括内置过滤以帮助防止不当消息发送。
实现消息回调Chat:InvokeChatCallback()``Class.Chat:RegisterChatCallback()Class.TextChatService.SendingMessage``Class.TextChatService.OnIncomingMessage遗产聊天系统绑定一个函数到聊天系统事件以发送消息。两种方法的 TextChatService 提供更好的灵活性和自定义。
添加自定义聊天命令ChatService/ChatCommand 模块TextChatCommandTextChatService 有一个专门用于文本命令的类而不是使用遗产聊天模块。
显示系统消息StarterGui:SetCore() 使用 ChatMakeSystemMessageTextChannel:DisplaySystemMessage()TextChannel.OnIncomingMessage 回调可以返回一个 TextChatMessageProperties 实例来自定义消息的外观。
禁用聊天游戏设置 在工作室和 ChatWindow/ChatSettings 模块隐藏聊天窗口ChatWindowConfiguration.Enabled

消息筛选

TextChatService 自动根据每个玩家的帐户信息过滤聊天消息,因此您不需要手动实现所有类型的聊天消息的文本过滤。

功能性遗产聊天文字聊天服务
为个人玩家过滤聊天消息Chat:FilterStringAsync()自动
过滤广播消息Chat:FilterStringForBroadcast()自动

窗口和泡泡聊天

both the 聊天窗口泡泡聊天 行为和自定义选项的 TextChatService 与遗产聊天系统相同。由于遗产聊天系统只允许使用聊天模块或 Players 容器进行自定义,服务提供了专用类 ( ChatWindowConfigurationBubbleChatConfiguration )来管理所有聊天窗口和泡泡聊天属性。此外,您可以使用工作室设置轻松调整和预览泡泡聊天的外观和行为属性,而不需要全部编写脚本。

功能性遗产聊天文字聊天服务
启用聊天窗口Class.Chat.LoadDefaultChat``Class.Players.ClassicChatChatWindowConfiguration.Enabled
启用泡泡聊天Class.Chat.BubbleChatEnabled``Class.Players.BubbleChatBubbleChatConfiguration.Enabled
设置聊天窗口属性Players:SetChatStyle()ChatWindowConfiguration
设置泡泡聊天属性Chat:SetBubbleChatSettings()``Class.Chat.BubbleChatSettingsChanged()``Class.Players.BubbleChat``Class.Players:SetChatStyle()BubbleChatConfiguration
启用 NPC 泡泡Chat:Chat()TextChatService:DisplayBubble()

迁移发言者“额外数据”

遗产 Lua 聊天系统允许开发人员在 SetExtraData 类上使用 Speaker。这些数据被用来格式化名称颜色、聊天颜色或为特定发言人应用名称标签。

遗产聊天系统 SetExtraData

-- 遗产聊天系统中设置额外数据的示例
ChatService.SpeakerAdded:Connect(function(playerName)
local speaker = ChatService:GetSpeaker(playerName)
speaker:SetExtraData("NameColor", Color3.fromRGB(255, 255, 55))
speaker:SetExtraData("ChatColor", Color3.fromRGB(212, 175, 55))
speaker:SetExtraData("Tags", {{TagText = "YourTagName", TagColor = Color3.fromRGB(0, 255, 0)}, {TagText = "OtherTagName", TagColor = Color3.fromRGB(255, 0, 0)}})
end)

TextChatService 没有直接等价于 SetExtraData 的。相反,使用 回调 例如 OnWindowAdded 来自定义基于消息 TextSource 的富文本的消息外观。

以下是使用 Player 对象上的属性访问遗产 Lua 聊天的“额外数据”的示例:

文字聊天服务设置属性

local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
player:SetAttribute("NameColor", Color3.fromRGB(255, 255, 55))
player:SetAttribute("ChatColor", Color3.fromRGB(212, 175, 55))
player:SetAttribute("isYourTag", true)
player:SetAttribute("isOtherTag", true)
end)

然后,您可以使用 OnChatWindowAdded 回调来根据玩家设置的属性自定义聊天窗口的外观:

在聊天窗口添加的 TextChatService

local TextChatService = game:GetService("TextChatService")
local Players = game:GetService("Players")
TextChatService.OnChatWindowAdded = function(textChatMessage)
local textSource = textChatMessage.TextSource
if textSource then
local player = Players:GetPlayerByUserId(textSource.UserId)
if player then
local overrideProperties = TextChatService.ChatWindowConfiguration:DeriveNewMessageProperties()
overrideProperties.PrefixText = textChatMessage.PrefixText
overrideProperties.Text = textChatMessage.Text
local nameColor = player:GetAttribute("NameColor")
if nameColor and typeof(nameColor) == "Color3" then
overrideProperties.PrefixTextProperties.TextColor3 = nameColor
end
local chatColor = player:GetAttribute("ChatColor")
if chatColor and typeof(chatColor) == "Color3" then
overrideProperties.TextColor3 = chatColor
end
local isYourTag = player:GetAttribute("isYourTag")
if isYourTag == true then
overrideProperties.PrefixText = `<font color='rgb(0, 255, 0)'>[YourTag]</font> {overrideProperties.PrefixText}`
end
local isOtherTag = player:GetAttribute("isOtherTag")
if isOtherTag == true then
overrideProperties.PrefixText = `<font color='rgb(255, 0, 0)'>[OtherTag]</font> {overrideProperties.PrefixText}`
end
return overrideProperties
end
end
return nil
end