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
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.
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.
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.
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)
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()?