What do you want to achieve?
I want to filter every message a player sends in my chat.
What is the issue?
Everything I type “hi”, “hello”, etc. gets Content Deleted (red = default chat, blue = broken chat)
What solutions have you tried so far?
I have tried:
I. Getting rid of replacing messages with Content Deleted and making them disappear, anything you type doesn’t show up at all.
Filter function & changing TextLabel’s text code:
local function FSFB(text, sender)
game.Chat:FilterStringForBroadcast(text, sender)
end
local chatMessage = Util.Create"TextLabel"
{
Text = FSFB(chatMessageDisplayText, this.SendingPlayer);
--...
Changing to Content Deleted code:
if chatMessage.Text == "Label" and chatMessageDisplayText ~= "Label" then
chatMessage.Text = string.rep(" ", numNeededSpaces) .. "[ Content Deleted ]"
end
What is the condition supposed to do, why are you comparing both texts with “Label”? The text you input isn’t “Label”, maybe that’s why the condition passes each time.
If you do want to compare the strings you can just compare them directly: chatMessage.Text ~= chatMessageDisplayText
Chat:FilterStringForBrodcast() is an old function (but not deprecated), there is a new function which is TextService:FilterStringAsync() which returns a FilterTextResult (idk why), in which you can call one of 3 functions, one of them you might be interested in is the GetNonChatStringForBrodcastAync() but this won’t respect is the player’s age (players under 13 have more restrictions, using this functions will filter text in safe chat even for users 13+), so instead, use the GetChatForUserAsync() function on all players and give them the proper results.
You have to do something like this (btw, wrap the functions in pcall cause they sometimes error)
game.ReplicatedStorage.Message.OnServerEvent:Connect(function(sender, text, receiver) -- note that the player who fires the event is always passed in first in a remote event
local ts = game:GetService("TextService")
local FilterTextResult
local success, err = pcall(function()
FilterTextResult = ts:FilterStringAsync(text, sender.UserId) -- could return a value to the pcall if you want
end)
if success then
local FilteredText
success, err = pcall(function()
FilteredText = TextFilterResult:GetChatForUserAsync(receiver.UserId)
end)
if success then
-- display the text
else
-- error handling (should use an empty string)
end
else
-- error handling again
end
end)
Edit: When displaying the message for all player, you could use the GetNonChatStringForBrodcastAsync instead of the GetChatForUserAsync or loop through the GetPlayers methods and use GetChatForUserAsync on every player (to respect safe chat and not put safe chat only)
Hey, I’m now getting “Argument 1 missing or nil” whenever I chat.
Code for ChatClient:
Text = game.ReplicatedStorage.Message:FireServer(this.SendingPlayer.UserId, chatMessageDisplayText, this.ReceivingPlayer);
Code for Event:
game.ReplicatedStorage.Message.OnServerEvent:Connect(function(sender, text, receiver) -- note that the player who fires the event is always passed in first in a remote event
local ts = game:GetService("TextService")
local FilterTextResult
local success, err = pcall(function()
FilterTextResult = ts:FilterStringAsync(text, sender.UserId) -- could return a value if you want
end)
if success then
local FilteredText
success, err = pcall(function()
FilteredText = FilterTextResult:GetChatForUserAsync(receiver.UserId)
end)
if success then
print("Success! Message: "..text)
else
print("Failure, error: "..err)
end
else
print("Failure, error: "..err)
end
end)
I am now getting this error on fire:
Script:10: attempt to index upvalue ‘receiver’ (a nil value)
Code:
game.ReplicatedStorage.Message.OnServerEvent:Connect(function(sender, text, receiver) -- note that the player who fires the event is always passed in first in a remote event
local ts = game:GetService("TextService")
local FilterTextResult
local success, err = pcall(function()
FilterTextResult = ts:FilterStringAsync(text, sender.UserId) -- could return a value if you want
end)
if success then
local FilteredText
success, err = pcall(function()
FilteredText = FilterTextResult:GetChatForUserAsync(receiver.UserId)
end)
if success then
print("Success! Message: "..text)
else
print("Failure, error: "..err)
end
else
print("Failure, error: "..err)
end
end)
Hey, I just tried using your code and I’m getting this error:
ChatClient code:
local chatMessage = Util.Create"TextLabel"
{
Text = game.ReplicatedStorage.Message:FireServer(chatMessageDisplayText, this.ReceivingPlayer);
Event code:
game.ReplicatedStorage.Message.OnServerEvent:Connect(function(sender, text, receiver) -- note that the player who fires the event is always passed in first in a remote event
local ts = game:GetService("TextService")
local FilterTextResult
local success, err = pcall(function()
FilterTextResult = ts:FilterStringAsync(text, sender.UserId) -- could return a value if you want
end)
if success then
local FilteredText
success, err = pcall(function()
local ReceiverUserId = game.Players:FindFirstChild(receiver) and game.Players[receiver].UserId
FilteredText = FilterTextResult:GetChatForUserAsync(ReceiverUserId)
end)
if success then
print("Success! Message: "..text)
else
print("Failure, error: "..err)
end
else
print("Failure, error: "..err)
end
end)
In the LocalScript, you are expecting a return value, but RemoteEvents never return a value, if you want a value, you should use RemoteFunctions instead, but if the text is displayed to multiple players, you would have to use the RemoteEvent:FireAllClients() methods. You should check here for an explanation of Text and Chat filtering on the Roblox Developer Hub. One last thing, in the client code, I see a flaw, and some performance issues. First of all, you aren’t parenting the TextLabel to anything, so it won’t show up. And for performance issues, it is normally better to use Instance.new() and set the parent agreement last, here is how it would look
local ChatMessage = Instance.new("TextLabel")
ChatMessage.Text = -- how you would display the text
-- you can set any other parameters here, but set the parent argument last, since it is the fastest way
ChatMessage.Parent = -- the parent