This example shows how to use the TextChatService API to design your own frontend. It reuses the default text channels from TextChatService.CreateDefaultTextChannels and is very simple compared to the default UI.
First, disable the default UI that comes with the TextChatService by setting the ChatWindowConfiguration.Enabled and ChatInputBarConfiguration.Enabled properties to false in the Studio Properties window.
Create a replacement for the chat input bar. This is the text box that emits messages when the user presses Enter.
Create a ScreenGui and parent it to the StarterGui.
Create a TextBox and parent it to the ScreenGui. To position the text box at the bottom-center of the screen, set the TextBox.AnchorPoint to Vector2.new(0.5, 1) and the TextBox.Position to UDim2.new(0.5, 0, 1, 0).
Create a LocalScript and parent it to the TextBox.
Add the following code to the LocalScript:
Client--!strictlocal TextChatService = game:GetService("TextChatService")-- RBXGeneral is the default public channel created by TextChatService.CreateDefaultTextChannelslocal 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-- Users expect the TextBox to be cleared after sending a messageTextBox.Text = ""endend)
Create a replacement for the chat window. This is the ScrollingFrame that displays messages as they are received from TextChatService.MessageReceived. This step also creates a UIListLayout to automatically layout the messages.
Create a ScreenGui and parent it to the StarterGui.
Create a ScrollingFrame and parent it to the ScreenGui.
Create a UIListLayout and parent it to the ScrollingFrame.
Create a LocalScript and parent it to the ScrollingFrame.
Add the following code to the LocalScript:
Client--!strictlocal TextChatService = game:GetService("TextChatService")-- addMessageGui will create a new TextLabel for each message receivedlocal function addMessageGui(textChatMessage: TextChatMessage)local isOutgoingMessage = textChatMessage.Status == Enum.TextChatMessageStatus.Sendinglocal parent = script.Parent-- First check if the message already exists in the chat window.-- TextChatService.MessageReceived will be called once when sending and once when the message is received from the server.local 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-- Start listening for incoming messagesTextChatService.MessageReceived:Connect(addMessageGui)
The final view in the Explorer should look like this:
When using the TextChatService.MessageReceived event, it is important to check if the message already exists in the chat window. This event is called once when sending and once when the message is received from the server.
Test your experience by sending messages in the chat input bar. You should see the messages appear in the chat window. You can expand on this example by adding features such as focusing the TextBox when a key is pressed or adding chat tags.