So I made a custom chat and turns out players can swear, how to fix it?
game.ReplicatedStorage.SendMessage.OnServerEvent:Connect(function(plr,message)
local success,errormessage = pcall(function()
local text = game:GetService("TextService"):FilterStringAsync(message,plr.UserId)
end)
if not success then
game.ReplicatedStorage.ReceieveMessage:FireAllClients(plr.Name,"####")
print(errormessage)
else
game.ReplicatedStorage.ReceieveMessage:FireAllClients(plr.Name,message)
end
end)
The server-side script,its not printing any error for swear words or not tagging it
game.ReplicatedStorage.SendMessage.OnServerEvent:Connect(function(plr,message)
local text
local success,errormessage = pcall(function()
text = game:GetService("TextService"):FilterStringAsync(message,plr.UserId)
end)
if not success then
--game.ReplicatedStorage.ReceieveMessage:FireAllClients(plr.Name,"####")
print(errormessage)
else
game.ReplicatedStorage.ReceieveMessage:FireAllClients(plr.Name,text)
end
end)
You just checked if there was an error, if wasn’t then you sent whole, not filtered message. This would fix this.
pcall is a callback. When you declare a local variable in the function passed to pcall it’s only accessible to that function. The above would fix it but I particularly don’t like the pattern of creating another upvalue when you can already return values out of pcall.
local success, result = pcall(function ()
return game:GetService("TextService"):FilterStringAsync(message, plr.UserId)
end)
Also just for the sake of UX if you can’t filter the string I’d recommend replacing all existing characters with hashtags instead of defaulting the message to four tags. You don’t have to but I’d prefer being able to at least imply how much someone had to say but couldn’t get through.
The [%w%p] represents any alphanumeric or punctuation character. It replaces all alphanumeric characters (0-9 A-Z) and punctuation with hashtags but leaves in other types of characters like spaces.
game.ReplicatedStorage.SendMessage.OnServerEvent:Connect(function(plr,message)
local success,errormessage = pcall(function()
return game:GetService("TextService"):FilterStringAsync(message,plr.UserId)
end)
if not success then
game.ReplicatedStorage.ReceieveMessage:FireAllClients(plr.Name,"####")
print(errormessage)
else
game.ReplicatedStorage.ReceieveMessage:FireAllClients(plr.Name,errormessage) -- dont mind about the name
end
end)