Everytime I use FilterStringForBroadcast, my text keeps getting #######/Content Deleted

  1. What do you want to achieve?
    I want to filter every message a player sends in my chat.

  2. What is the issue?
    Everything I type “hi”, “hello”, etc. gets Content Deleted (red = default chat, blue = broken chat)
    image

  3. 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

Any help would be greatly appreciated.

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

2 Likes

I tried your solution, and this gave the same result. Here’s my code:

if chatMessage.Text ~= chatMessageDisplayText then
	chatMessage.Text = string.rep(" ", numNeededSpaces) .. "[ Content Deleted ]"
end

If I try to remove this condition to just use #####, it doesn’t put anything in chat.

If you want to make it simple, you could filter it then just add a check if it has any #'s in it.

2 Likes

I just tried this, however, no messages show if I type anything, but if I type anything with # in it, it gets replaced with [ Content Deleted ]

if string.find(chatMessageDisplayText, "#") then
	chatMessage.Text = string.rep(" ", numNeededSpaces) .. "[ Content Deleted ]"
end

image

[ Content Deleted ] was a hashtag that I put in.

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.

2 Likes

I treid using FilterStringAsync, nothing appears in chat now.

game.ReplicatedStorage.Message.OnServerEvent:Connect(function(text, sender, receiver)
	local ts = game:GetService("TextService")
	ts:FilterStringAsync(text, sender, receiver)
end)
Text = game.ReplicatedStorage.Message:FireServer(chatMessageDisplayText, this.SendingPlayer.UserId, this.ReceivingPlayer)

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)

1 Like

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)

the player is already sent through a remote event automatically, so the client should do this

game.ReplicatedStorage.Message:FireServer(chatMessageDisplayText, this.ReceivingPlayer)

also note that FireServer returns no value, so it shouldn’t be stored in a variable

1 Like

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)

is your client code handling the receiver, I would recommend you use 2 textbooks, one for handling the receiver and one for the actual message

1 Like

Also, the receiver must be a player, not a string, if you are getting a string, you should use do

local ReceiverUserId = game.Players:FindFirstChild(receiver) and game.Players[receiver].UserId

and use this value as the UserId of the receiving player

1 Like

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
1 Like