So whenever I try filtering my chat message, there are 2 issues.
Here is my code.
Client:
01 | script.Parent.FocusLost:Connect( function (enter) |
02 | local enteredText = script.Parent.Text |
03 | if enter then |
04 |
05 | script.Parent.Text = "" |
06 |
07 | game.ReplicatedStorage.Events.FilterChatMessages:FireServer(enteredText) |
08 |
09 | game.ReplicatedStorage.Events.SendFilteredMessage.OnClientEvent:Connect( function (filteredText) |
10 | -- note: this part works and puts the text on my screen! |
11 | local text = Instance.new( "TextLabel" ) |
12 | text.RichText = true |
13 | text.BorderSizePixel = 0 |
14 | text.BackgroundColor 3 = Color 3. fromRGB( 180 , 180 , 180 ) |
15 | text.TextColor 3 = Color 3. fromRGB( 255 , 255 , 255 ) |
Server:
01 | local textService = game:GetService( "TextService" ) |
02 |
03 | local filteredText |
04 | local result |
05 |
06 | game.ReplicatedStorage.Events.FilterChatMessages.OnServerEvent:Connect( function (player, text) |
07 | local success, errorMessage = pcall ( function () |
08 | filteredText = textService:FilterStringAsync(text, player.UserId) |
09 | end ) |
10 |
11 | if not success then |
12 | filteredText = "[Content Deleted]" |
13 | print (errorMessage) |
14 | end |
15 |
Please help!
You keep connecting the OnClientEvent each time you send a message, and this is a problem because when you send a message for the n-th time, n messages are sent to the server (one for each connection).
This type of mistake is most common in while
loops, where people keep connecting a function inside the loop. For most people it doesn't matter until they have a gargantuan amount of them which causes memory leak and eventually lots of lag.
The solution? Simply keep the OnClientEvent
function in it's own scope. Removed from the text-box's. Like so:
1 | TextBox.FocusLost:Connect( function (enterPressed) |
2 | if enterPressed then |
3 | RemoteEvent:FireServer(TextBox.Text) |
4 | end |
5 | end ) |
6 |
7 | RemoteEvent.OnClientEvent:Connect( function (filteredMessage) |
8 | -- do stuff |
9 | end ) |
As an aside, you should do :FireAllClients()
instead of FireClient()
; if you want the message to go to every player.
Try testing in-game since studio disables all filtering.