So whenever I try filtering my chat message, there are 2 issues.
Here is my code.
Client:
script.Parent.FocusLost:Connect(function(enter) local enteredText = script.Parent.Text if enter then script.Parent.Text = "" game.ReplicatedStorage.Events.FilterChatMessages:FireServer(enteredText) game.ReplicatedStorage.Events.SendFilteredMessage.OnClientEvent:Connect(function(filteredText) -- note: this part works and puts the text on my screen! local text = Instance.new("TextLabel") text.RichText = true text.BorderSizePixel = 0 text.BackgroundColor3 = Color3.fromRGB(180, 180, 180) text.TextColor3 = Color3.fromRGB(255, 255, 255) text.BackgroundTransparency = 0.5 text.Text = '<b><i><font color="rgb(255,165,0)">'..game.Players.LocalPlayer.DisplayName..'; </font> '..filteredText..'</i></b>' text.Parent = script.Parent.Parent.ChatArea wait() text.Size = UDim2.new(0, text.TextBounds.X, 0, text.TextBounds.Y) end) end end)
Server:
local textService = game:GetService("TextService") local filteredText local result game.ReplicatedStorage.Events.FilterChatMessages.OnServerEvent:Connect(function(player, text) local success, errorMessage = pcall(function() filteredText = textService:FilterStringAsync(text, player.UserId) end) if not success then filteredText = "[Content Deleted]" print(errorMessage) end if filteredText:IsA("TextFilterResult") then result = filteredText:GetNonChatStringForBroadcastAsync() if result ~= text then print(result) print(text) result = "[Content Deleted]" end end game.ReplicatedStorage.Events.SendFilteredMessage:FireClient(player, result) end)
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:
TextBox.FocusLost:Connect(function(enterPressed) if enterPressed then RemoteEvent:FireServer(TextBox.Text) end end) RemoteEvent.OnClientEvent:Connect(function(filteredMessage) -- do stuff 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.