Este exemplo mostra como usar a classe TextChatService para projetar seu próprio frontend. Ele reutiliza os canais de texto padrão do CreateDefaultTextChannels e é muito simples em comparação com a UI padrão.
Desative a UI padrão que vem com o TextChatService definindo 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 parent para StarterGui.
Crie um TextBox e o parent para o novo ScreenGui, então reposicione e redimensione conforme desejado.
Crie um LocalScript e o parent para o 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. Este é o ScrollingFrame que exibe mensagens à medida que são recebidas do TextChatService.MessageReceived. Este passo também cria um UIListLayout para organizar automaticamente as mensagens.
Crie outro novo ScreenGui e o parent para StarterGui.
Crie um ScrollingFrame e o parent para o ScreenGui, então reposicione e redimensione conforme desejado.
Crie um UIListLayout e o parent para o ScrollingFrame.
Crie um LocalScript e o parent para o 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 ouvir 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 o TextBox quando uma tecla é pressionada ou adicionar tags de chat.