Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Custom chat system not filtering text and duplicating messages?

Asked by 3 years ago

So whenever I try filtering my chat message, there are 2 issues.

  1. It doesn't filter it
  2. It duplicates the message whenever it puts it in the chat area (if it is the 3rd message I make, there are 3 copies, and not one.)

Here is my code.

Client:

01script.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.BackgroundColor3 = Color3.fromRGB(180, 180, 180)
15            text.TextColor3 = Color3.fromRGB(255, 255, 255)
View all 23 lines...

Server:

01local textService = game:GetService("TextService")
02 
03local filteredText
04local result
05 
06game.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 
View all 26 lines...

Please help!

2 answers

Log in to vote
1
Answered by 3 years ago

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:

1TextBox.FocusLost:Connect(function(enterPressed)
2    if enterPressed then
3        RemoteEvent:FireServer(TextBox.Text)
4    end
5end)
6 
7RemoteEvent.OnClientEvent:Connect(function(filteredMessage)
8    -- do stuff
9end)

As an aside, you should do :FireAllClients() instead of FireClient(); if you want the message to go to every player.

0
sorry for responding late, i'll try this sometime soon, rn i just don't feel like working on it lol FirewolfYT_751 223 — 3y
0
If it works then accept my answer. radiant_Light203 1166 — 3y
0
thanks it works! i do have another question but i'm gonna make another question for that since its mostly unrelated to this. FirewolfYT_751 223 — 3y
Ad
Log in to vote
0
Answered by
3F1VE 257 Moderation Voter
3 years ago

Try testing in-game since studio disables all filtering.

0
ok, but what about the fact that it duplicates the message in such a way, that it creates the chat message 3 times rather than once if it's the 3rd message FirewolfYT_751 223 — 3y
0
I can't really help you with that, I have never worked with custom chat. 3F1VE 257 — 3y
0
I can't really help you with that, I have never worked with custom chat. 3F1VE 257 — 3y

Answer this question