How do you Chat Filter with a custom made chat?

How can I filtered text from my code to the game?

I tried to make a filtering on the Custom Chat that I have created but the filtering don’t seem to work and I don’t have a clue on what is wrong.

The Code Below Is a Server Script.

local RS = game:GetService("ReplicatedStorage")
local Event = game.ReplicatedStorage.Events:WaitForChild('ChatEvent')
local TextService = game:GetService("TextService")

local function Filter(msg, plr)
	local filter
	local success, failed = pcall(function()
		filter = TextService:FilterStringAsync(msg, plr)
	end)
	if success then
		return filter
	end
	return false
end

local function getFilteredMSG(text, recipient)
	local result
	local success, err = pcall(function()
		result = text:GetChatForUserAsync(recipient)
	end)
	if success then
		return result
	end
	return false
end

local function Send(plr, msg)
	if msg ~= '' then
		local filteredmsg = Filter(msg, plr.UserId)
		if filteredmsg then
			for i, v in pairs(game.Players:GetPlayers()) do
			local filteredmsg = getFilteredMSG(Filter, v.UserId)
				Event:FireAllClients(msg, plr.Name)
		end
end 
	end
end

Event.OnServerEvent:Connect(Send)

I tried replacing built in functions and placing scripts into the GUI but it still did not work out

My Code is suppose to filter texts when a new template is Cloned

Template

2 Likes

Your Send() function has an error.

-- this is wrong, should be throwing an error
if msg ~= " then
-- should be
if msg ~= "" then

I’d also recommend fixing your tabing and to format your code when you post on the devforum, so its easier for others to help you

A good way to filtering message is by using a function in TextService either way here is a topic that explains it very well

1 Like

Do you require the 3rd Parameter in FilterStringAsync() or leave it blank

I have tried replacing the (if msg ~= “” then) but the code still doesn’t filter the text, I also checked by making the Filter function print the success pcall and when it print filtered, the text is still not filtered in the game.

1 Like
local RS = game:GetService("ReplicatedStorage")
local Chat = game:GetService('Chat')
local Event = game.ReplicatedStorage.Events:WaitForChild('ChatEvent')
local TextService = game:GetService("TextService")

local function FilterMessage(msg, plr)
	local filter
	local success, failed = pcall(function()
		filter = TextService:FilterStringAsync(msg, plr)
	end)
	if success then
		print("Sentence Filtered: "..msg)
		return filter
	end
	return false
end

local function getFilteredMessage(text, recipient)
	local result
	local success, err = pcall(function()
		result = text:GetChatForUserAsync(recipient)
	end)
	if success then
		return result
	end
	return false
end


local function onSendMessage(plr, msg)
	if msg ~= "" then
		local FilteredMessage = FilterMessage(msg, plr.UserId)
		if FilteredMessage then
			for i, v in pairs(game.Players:GetPlayers()) do
				local FilteredMessage = getFilteredMessage(FilterMessage, v.UserId, plr)
				Event:FireAllClients(msg, plr.Name)
		end
end 
	end
end


Event.OnServerEvent:Connect(onSendMessage)

I have fixed the code up a bit but it still does not filter.

1 Like

Have you tried using it in the game servers instead of just in studio?

1 Like

You do know that when you printed “Sentence Filtered” you printed the unfiltered message since you used the msg variable. Instead you would need to print filter in order to print the filtered message.

Yes I have tried playing it in the game servers

1 Like

When I try printing the filter, it gave me an output of Instance
what does that mean?

1 Like

Try using a LocalScript instead and put the script on the client.

Never mind, that’ll complicate things. What that means is that filter is actually an Instance for some odd reason. Can you repost your code?

The reason it’s printing Instance is because filter is an instance in that function since it’s a TextObject. Sorry if I seem unconfident about this stuff, it’s because I haven’t made a filtering text script myself before so I am learning as we go along from the hub.

local RS = game:GetService("ReplicatedStorage")
local Chat = game:GetService('Chat')
local Event = game.ReplicatedStorage.Events:WaitForChild('ChatEvent')
local TextService = game:GetService("TextService")
local PJ = game.ReplicatedStorage.Events:WaitForChild("PJ")
game.Players.PlayerAdded:Connect(function(plr)
	print(plr.Name..' Joined the game')
	PJ:FireAllClients(plr.Name)
end)

local function FilterMessage(msg, plr)
	local filter
	local success, failed = pcall(function()
		filter = TextService:FilterStringAsync(msg, plr)
	end)
	if success then
		print(filter)
		return filter
	end
	return false
end

local function getFilteredMessage(text, recipient)
	local result
	local success, err = pcall(function()
		result = text:GetChatForUserAsync(recipient)
	end)
	if success then
		return result
	end
	return false
end


local function onSendMessage(plr, msg)
	if msg ~= "" then
		local FilteredMessage = FilterMessage(msg, plr.UserId)
		if FilteredMessage then
			for i, v in pairs(game.Players:GetPlayers()) do
				
				local FilteredMessage = getFilteredMessage(FilterMessage, v.UserId, plr)
				print(FilteredMessage)
				Event:FireAllClients(msg, plr.Name)
		end
end 
	end
end


Event.OnServerEvent:Connect(onSendMessage)

this this the repost of the code

How do you know the filtering system doesn’t work? This should work by the looks of it. Have you tried printing the filtered text at the end of it inside onSendMessage()?

What does it print when you print FilteredMessage?

The Filtered message printed False

That means it failed somewhere a lot the way… Most likely at getFilteredMessage it seems.

i dont really know what GetChatForUserAsync() is so i think that is the problem.

First off, I don’t understand why you have a 3rd argument plr inside of getFilteredMessage when you have only 2 parameters defined.