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 2 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:

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!

2 answers

Log in to vote
1
Answered by 2 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:

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.

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 — 2y
0
If it works then accept my answer. radiant_Light203 1166 — 2y
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 — 2y
Ad
Log in to vote
0
Answered by
3F1VE 257 Moderation Voter
2 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 — 2y
0
I can't really help you with that, I have never worked with custom chat. 3F1VE 257 — 2y
0
I can't really help you with that, I have never worked with custom chat. 3F1VE 257 — 2y

Answer this question