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.
Desabilite a interface padrão que vem com o TextChatService desativando as propriedades ChatWindowConfiguration.Enabled e ChatInputBarConfiguration.Enabled para false na janela Propriedades do Studio .
Crie uma substituição 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 associá-lo a StarterGui .
Crie um TextBox e associá-lo ao novo ScreenGui , então reposicione-o e redimensione-o como desejado.
Crie um LocalScript e associá-lo ao novo TextBox.
Adicione o seguinte código ao LocalScript :
Clientelocal 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("Failed to send message")end-- Os usuários esperam que a caixa de entrada seja limpa após enviar uma mensagemtextBox.Text = ""endend)
Crie uma substituição para a janela de chat.Este é o ScrollingFrame que exibe mensagens conforme são recebidas de TextChatService.MessageReceived.Este passo também cria um UIListLayout para organizar automaticamente as mensagens.
Crie outro novo ScreenGui e associá-lo a StarterGui .
Crie um ScrollingFrame e associá-lo ao ScreenGui , então reposicione-o e redimensione-o como desejado.
Crie um UIListLayout e associá-lo ao ScrollingFrame.
Crie um LocalScript e associá-lo ao ScrollingFrame.
Adicione o seguinte código ao LocalScript :
Clientelocal TextChatService = game:GetService("TextChatService")-- Função para criar uma nova etiqueta 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 sua experiência enviando mensagens na barra de entrada de chat.Você deve ver as mensagens aparecerem na janela de chat.Você pode então expandir esse exemplo adicionando recursos como focar o quando uma tecla é pressionada ou adicionando tags de bate-papo .