Ví dụ này cho thấy cách sử dụng lớp TextChatService để thiết kế giao diện trước của riêng bạn.Nó tái sử dụng các kênh văn bản mặc định từ CreateDefaultTextChannels và rất đơn giản so với UI mặc định.
Vô hiệu hóa giao diện mặc định đi kèm với TextChatService bằng cách đặt các thuộc tính ChatWindowConfiguration.Enabled và ChatInputBarConfiguration.Enabled về false trong cửa sổ Tính năng của Studio .
Tạo một thay thế cho thanh nhập trò chuyện. Đây là hộp văn bản phát ra tin nhắn khi người dùng nhấn Enter .
Tạo một ScreenGui và gán nó cho StarterGui .
Tạo một TextBox và cha nó cho mới ScreenGui , sau đó di chuyển lại và thay đổi kích thước như mong muốn.
Tạo một LocalScript và cha nó cho mới TextBox .
Thêm mã sau vào LocalScript :
Khách hànglocal TextChatService = game:GetService("TextChatService")-- RBXGeneral là kênh công cộng mặc địnhlocal RBXGeneral = TextChatService:FindFirstChild("TextChannels"):WaitForChild("RBXGeneral")local textBox = script.ParenttextBox.FocusLost:Connect(function(enterPressed)local text = textBox.Textif enterPressed and #text > 0 thenlocal success, response = pcall(function()return RBXGeneral:SendAsync(textBox.Text)end)if not success thenRBXGeneral:DisplaySystemMessage("Failed to send message")end-- Người dùng mong đợi hộp nhập sẽ được xóa sau khi gửi tin nhắntextBox.Text = ""endend)
Tạo một thay thế cho cửa sổ trò chuyện.Đây là ScrollingFrame mà hiển thị tin nhắn như chúng được nhận từ TextChatService.MessageReceived .Bước này cũng tạo ra một UIListLayout để tự động bố trí các tin nhắn.
Tạo một mới khác ScreenGui và gán nó cho StarterGui .
Tạo một ScrollingFrame và cha nó cho ScreenGui , sau đó di chuyển lại và thay đổi kích thước như mong muốn.
Tạo một UIListLayout và gán nó cho ScrollingFrame .
Tạo một LocalScript và gán nó cho ScrollingFrame .
Thêm mã sau vào LocalScript :
Khách hànglocal TextChatService = game:GetService("TextChatService")-- Chức năng tạo nhãn văn bản mới cho mỗi tin nhắn được nhậnlocal function addMessageGui(textChatMessage: TextChatMessage)local isOutgoingMessage = textChatMessage.Status == Enum.TextChatMessageStatus.Sendinglocal parent = script.Parentlocal originalLabel = parent:FindFirstChild(textChatMessage.MessageId)if originalLabel thenoriginalLabel.Text = textChatMessage.TextoriginalLabel.BackgroundTransparency = if isOutgoingMessage then 0.5 else 0elselocal textLabel = Instance.new("TextLabel")textLabel.BorderSizePixel = 0textLabel.Font = Enum.Font.BuilderSanstextLabel.TextSize = 18textLabel.TextXAlignment = Enum.TextXAlignment.LefttextLabel.BackgroundTransparency = if isOutgoingMessage then 0.5 else 0textLabel.BackgroundColor3 = Color3.fromRGB(0, 0, 0)textLabel.TextColor3 = Color3.fromRGB(255, 255, 255)textLabel.Name = textChatMessage.MessageIdtextLabel.AutomaticSize = Enum.AutomaticSize.XYtextLabel.Text = textChatMessage.TexttextLabel.Parent = parentendend-- Bắt đầu lắng nghe các tin nhắn đếnTextChatService.MessageReceived:Connect(addMessageGui)
Thử trải nghiệm của bạn bằng cách gửi tin nhắn trong thanh nhập trò chuyện.Bạn nên thấy các tin nhắn xuất hiện trong cửa sổ trò chuyện.Bạn có thể mở rộng ví dụ này bằng cách thêm các tính năng như tập trung vào TextBox khi nhấn phím hoặc thêm thẻ trò chuyện .