Text is not getting filtered

Hello developers!

I need some help with a script that filters text. I took a script from the Text and Chat Filtering page on the Developer Hub. If anybody can help me or tell me why my script isn’t filtering text then I would be very happy!


Scripts

Script

-- Service
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TextService = game:GetService("TextService")
local ShoutCommandEvent = ReplicatedStorage:WaitForChild("AdminEvent").ShoutCommandEvent
local ShoutClientCommandEvent = ReplicatedStorage:WaitForChild("AdminEvent").ShoutClientCommandEvent
--
ShoutCommandEvent.OnServerEvent:Connect(function(player, ShoutText)
	local filteredTextResult
	local success, errorMessage = pcall(function()
		filteredTextResult = TextService:FilterStringAsync(ShoutText, player.UserId)
	end)
	if success then
		ShoutClientCommandEvent:FireAllClients(player, ShoutText)
	elseif not success then
		warn(errorMessage)
	end
end)

Local Script

-- Service
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ShoutCommandEvent = ReplicatedStorage:WaitForChild("AdminEvent").ShoutCommandEvent
local ShoutClientCommandEvent = ReplicatedStorage:WaitForChild("AdminEvent").ShoutClientCommandEvent
-- Locals
local ShoutButton = script.Parent.Parent.Background.ServerPlayerCommands.ServerShout.ShoutButton
local ShoutText = script.Parent.Parent.Background.ServerPlayerCommands.ServerShout.TextBox
local Text = script.Parent.Parent.ShoutFrame.TextLabel
--
ShoutButton.MouseButton1Click:Connect(function()
	ShoutCommandEvent:FireServer(ShoutText.Text)
end)

ShoutClientCommandEvent.OnClientEvent:Connect(function(player, FilterText)
	Text.Text = FilterText
end)

In the server script, you’re supposed to FireAllClients with the filtered text result that TextService:FilterStringAsync() returned, like this:

-- Service
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TextService = game:GetService("TextService")
local ShoutCommandEvent = ReplicatedStorage:WaitForChild("AdminEvent").ShoutCommandEvent
local ShoutClientCommandEvent = ReplicatedStorage:WaitForChild("AdminEvent").ShoutClientCommandEvent
--
ShoutCommandEvent.OnServerEvent:Connect(function(player, ShoutText)
	local filteredTextResult
	local success, errorMessage = pcall(function()
		filteredTextResult = TextService:FilterStringAsync(ShoutText, player.UserId)
	end)
	if success then
		ShoutClientCommandEvent:FireAllClients(player, filteredTextResult)
	elseif not success then
		warn(errorMessage)
	end
end)

Also, when testing in studio, as you can see when you type in chat, there is no filter.

1 Like

I tryed the script but I got an error

Unable to assign property Text. string expected, got nil

I don’t know why it isn’t a string.

Hello! I reccomend you don’t use TextService, and instead use Chat, and filter it server side.

local ChatService = game:GetService(“Chat”)
local UnfilteredString = “” --unfiltered string
local Player = Player -- player that sent the message goes here

local FilteredString = ChatService:FilterStringForBroadcast(UnfilteredString, Player)
1 Like