I have a very basic chat system set up with two remotes. One remote is fired whenever a player chats through a TextBox which is sent to the server, which sends the message and sender to every other client. The message is then displayed. This is what the scripts are:
Server:
local repstorage = game.ReplicatedStorage
local chatRecieved = repstorage:WaitForChild("ChatRecieved")
local chatted = repstorage:WaitForChild("Chatted")
chatted.OnServerEvent:Connect(function(player,message)
print("recieved",player,message)
chatRecieved:FireAllClients(player,message)
end)
Client:
-- send messages to the server
chatBox.FocusLost:Connect(function(enterPressed)
if enterPressed then
chatted:FireServer(chatBox.Text)
print("chatted:",chatBox.Text)
chatBox.Text = ""
end
end)
chatRecieved.OnClientEvent:Connect(function(player, message)
-- print("uhhh we received a message")
local newMessage = messageItem:Clone()
local chatColor = nameColor:GetNameColor(player.Name)
local hexColor = chatColor:ToHex()
newMessage.Text = `<mark color="#{hexColor}" transparency="0.5"><font color="#FF0000">{player.Name}: </font>{message}</mark>`
-- print("text created")
newMessage.Parent = chatFrame
debris:AddItem(newMessage, 6)
print("success")
end)
My concern is that if I filter on the client, the client will be able to see the unfiltered message which is against TOS. If i filter on the server, I cannot filter it differently for over or under 13 users.
First of all, you cannot use the chat filter functions from the client, but moreover, you cannot use TextService:FilterStringAsync() for chat related messages, without breaking the ToS (and GetChatForUserAsync() is deprecated and returns an empty string)
Chat filtering must go through TextChatService (TextChannel:SendAsync() more specifically). You can read about the migration to TextChatService and this new requirement here:
Like the previous post says, TextChannel:SendAsync will handle privacy settings, filtering, and message delivery for you. TextChatService.CreateDefaultTextChannels can be set to false if you want to completely manage all TextChannels in your experience. Alternatively, you can also create additional TextChannels alongside the default ones if needed. TextChannels need to be a descendant of TextChatService to function.
To enroll a user into a TextChannel, on the server you can use TextChannel:AddUserAsync.
You can disable the default UI by setting TextChatService.ChatWindowConfiguration.Enabled = false and TextChatService.ChatInputBarConfiguration.Enabled = false to supplement with your own UI.
You can listen for messages per TextChannel using TextChannel.MessageReceived
If you have any additional question on how to utilize TextChatService for your usecase, refer to the online documentation or feel free to reply back with any questions
In the documentation, I can’t find any info on creating or using text channels. The TextChannel page wasn’t much help either.
I had to fork the channels directly from TextChatService.
SendAsync() is not a filtering function per say, It sends the chat message to TextChatService’s internal systems. In between that and the MessageReceived event being fired, the message goes to the server (where it gets filtered) and then goes to multiple clients, firing multiple events and callbacks along the way
—-
You can create a TextChannel using Instance.new(“TextChannel”)
I actually decided to use the default text channel for this. This is my code sample.
local channels = textchatservice:WaitForChild("TextChannels")
local channel:TextChannel = channels:WaitForChild("RBXGeneral")
chatRecieved.OnClientEvent:Connect(function(player, message)
-- print("uhhh we received a message")
local filteredMessage = channel:SendAsync(message)
local text = filteredMessage.Text
The “text” variable returns the message, but it is unfiltered.
You shouldnt need any RemoteEvents to accomplish this goal. TextChannel acts as a replicator of messages already.
-- from client
TextBox.FocusLost:Connect(function(enterPressed)
TextChannel:SendAsync(TextBox.Text) -- any partiipants of this TextChannel could receive this message
end)
TextChannel.MessageReceived:Connect(function(textChatMessage)
print("Got message: " .. textChatMessage.Status .. " " .. textChatMessage.Text)
-- for outgoing messages, the first textChatMessage will have a Status of "Sending" and wont yet be filtered.
-- the second textChatMessage will return from the server, will likely have the "Success" status, and will be filtered
end)
i’m not sure if I’m doing this right, but the print message did not show up on the messageReceived event.
Client:
local textchatservice = game:GetService("TextChatService")
local channels = textchatservice:WaitForChild("TextChannels")
local channel:TextChannel = channels:WaitForChild("RBXGeneral")
chatBox.FocusLost:Connect(function(enterPressed)
if enterPressed then
channel:SendAsync(chatBox.Text)
print("sent")
chatBox.Text = ""
end
end)
Server:
local textchatservice = game:GetService("TextChatService")
local channels = textchatservice:WaitForChild("TextChannels")
local channel:TextChannel = channels:WaitForChild("RBXGeneral")
channel.MessageReceived:Connect(function(message)
print("message recieved: "..message.Status.." "..message.Text)
end)
MessageReceived is a client only event. Due to the fact filtering can be different depending on the receiving player, the server cannot receive a filtered version of the message
Rule of thumb is that, with TextChatService, almost everything is on the client (for the best and the worst…). There are only some rare exceptions (mainly TextChannel.ShouldDeliverCallback)
I’m not sure what the TextChatMessage being returned is (or in which state it is), the documentation is not very clear about that, but that is not the intended way to do it. Out of curiosity, you could print the TextChatMessage.Status of the TextChatMessage
The chat filter only works on roblox servers (because roblox doesn’t want their chat filter to run on people’s pc’s or something, to avoid reverse engineering of the filter probably). If you have team create enabled, you can go in team test, I think that works (because the server runs on roblox’s servers instead of your pc), or you can test it in a live game