Este exemplo mostra como usar a classe TextChatService para projetar seu próprio frontend. Ele reutiliza os canais de texto padrão de CreateDefaultTextChannels e é muito simples em comparação com a interface padrão.
Desative a interface padrão que vem com o TextChatService configurando as propriedades ChatWindowConfiguration.Enabled e ChatInputBarConfiguration.Enabled como false na janela Propriedades do Studio.
Crie um substituto para a barra de entrada de chat. Esta é a caixa de texto que emite mensagens quando o usuário pressiona Enter.
Crie um ScreenGui e o coloque como filho de StarterGui.
Crie um TextBox e o coloque como filho do novo ScreenGui, depois reposicione e redimensione conforme desejado.
Crie um LocalScript e o coloque como filho do novo TextBox.
Adicione o seguinte código ao LocalScript:
Clientlocal TextChatService = game:GetService("TextChatService")-- RBXGeneral é o canal público padrãolocal 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("Falha ao enviar mensagem")end-- Os usuários esperam que a caixa de entrada seja limpa após enviar uma mensagemtextBox.Text = ""endend)
Crie um substituto para a janela de chat. Esta é a ScrollingFrame que exibe mensagens à medida que são recebidas do TextChatService.MessageReceived. Esta etapa também cria um UIListLayout para layout automático das mensagens.
Crie outro novo ScreenGui e o coloque como filho de StarterGui.
Crie um ScrollingFrame e o coloque como filho do ScreenGui, depois reposicione e redimensione conforme desejado.
Crie um UIListLayout e o coloque como filho do ScrollingFrame.
Crie um LocalScript e o coloque como filho do ScrollingFrame.
Adicione o seguinte código ao LocalScript:
Clientlocal TextChatService = game:GetService("TextChatService")-- Função para criar um novo rótulo de texto para cada mensagem recebidalocal 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-- Comece a escutar por mensagens recebidasTextChatService.MessageReceived:Connect(addMessageGui)
Teste seu jogo enviando mensagens na barra de entrada de chat. Você deve ver as mensagens aparecerem na janela de chat. Você pode então expandir este exemplo adicionando recursos, como focar no TextBox quando uma tecla é pressionada ou adicionar tags de chat.