Ví dụ này cho thấy cách sử dụng lớp TextChatService để thiết kế giao diện người dùng 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 giao diện mặc định.
Vô hiệu hóa giao diện mặc định đi kèm với TextChatService bằng cách thiết lập thuộc tính ChatWindowConfiguration.Enabled và ChatInputBarConfiguration.Enabled thành false trong cửa sổ Thuộc tính của Studio.
Tạo một thay thế cho thanh nhập văn bản trò chuyện. Đây là hộp văn bản phát đi các tin nhắn khi người dùng nhấn Enter.
Tạo một ScreenGui và làm cha cho nó là StarterGui.
Tạo một TextBox và làm cha nó cho ScreenGui mới, sau đó định vị lại và thay đổi kích thước theo mong muốn.
Tạo một LocalScript và làm cha cho nó là TextBox mới.
Thêm mã sau vào LocalScript:
Clientlocal 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("Gửi tin nhắn không thành công")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 hiển thị các tin nhắn khi chúng được nhận từ TextChatService.MessageReceived. Bước này cũng tạo một UIListLayout để tự động sắp xếp các tin nhắn.
Tạo một ScreenGui mới khác và làm cha cho nó là StarterGui.
Tạo một ScrollingFrame và làm cha cho ScreenGui, sau đó định vị lại và thay đổi kích thước theo mong muốn.
Tạo một UIListLayout và làm cha cho nó là ScrollingFrame.
Tạo một LocalScript và làm cha cho nó là ScrollingFrame.
Thêm mã sau vào LocalScript:
Clientlocal TextChatService = game:GetService("TextChatService")-- Hàm để tạo một nhãn văn bản mới cho mỗi tin nhắn nhận đượclocal 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)
Kiểm tra trò chơi của bạn bằng cách gửi tin nhắn trong thanh nhập văn bản trò chuyện. Bạn sẽ thấy các tin nhắn xuất hiện trong cửa sổ trò chuyện. Sau đó, 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 một phím được nhấn hoặc thêm thẻ trò chuyện.