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

[Solved]: How do I Add String Filtering Function in my Script?

Asked by 2 years ago
Edited 2 years ago

Hello my kind scripters,

I had a simple chat script in ServerScriptService. I made it so that client sent a message through text box and the Server-Sided script would receive it and send it back to all clients. The script in ServerScriptService also adds cool feature like; System messages, Devs and Owner messages etc. I need help setting a Message filtering function to tag inappropriate messages in the Server-Sided script

The Local Script

local serverMessageColor = Color3.fromRGB(255, 255, 0)
local Event = game.ReplicatedStorage.ServerMessageSystem
local SentBack = game.ReplicatedStorage.BackToClient
--==================================================
-- sends message from textbox when pressed Enter
script.Parent.FocusLost:Connect(function(Enter)

    if Enter then
        Event:FireServer(script.Parent.Text)
    end
    script.Parent.Text = ""
end)
--==========================
-- adds messages to scrolling frame and deletes oldest one when there are 17 messages
function Newlabel(text,bool,color) 
    if bool == false then
    local MessageNew = game.ReplicatedStorage.NewMessage:Clone()
        MessageNew.Text = text
        MessageNew.TextColor3 = color
    MessageNew.Parent = script.Parent.Parent.ScrollingFrame
        if #script.Parent.Parent.ScrollingFrame:GetChildren() >= 17 then
            for i,v in pairs (script.Parent.Parent.ScrollingFrame:GetChildren()) do
                if v.Name ~= "LocalScript" and v.Name ~= "UIListLayout" and i == 3 then
                    v:Destroy() 
                end
            end
        end
--==========================
-- System message; color is yellow.
    elseif bool == true then
        local MessageNew = game.ReplicatedStorage.NewMessage:Clone()
        MessageNew.Text = text
        MessageNew.Parent = script.Parent.Parent.ScrollingFrame
        MessageNew.TextColor3 = serverMessageColor

    end
end
--======================================
SentBack.OnClientEvent:Connect(Newlabel)

The local script above gets the message in text box and sends the name and message to the script in ServerScriptService by remote events. It also receives the messages through remote events from the script in ServerScriptService back to the client and adds a new text label with the name and message and also colors the System messages.

The Script in ServerScriptService

local Event = game.ReplicatedStorage.ServerMessageSystem
local SendAllEvent = game.ReplicatedStorage.BackToClient
local DevTextMessageColor = Color3.fromRGB(255, 0, 0)
local NormalMessageColor = Color3.fromRGB(255, 255, 255)
local color
local text2
--=========================================
-- if the name is in devs it makes devs specific message
function NewMessageDisplay(player, text) 
    if player.Name == "ABDULGHANI1010" or player.Name == "ImAGoodBoy12317" then
        color = DevTextMessageColor
        text2 = "[Dev]["..player.Name.."]: "..text
    else
if name is not in devs then makes a normal message
        color = NormalMessageColor
        text2 = "["..player.Name.."]: "..text
    end

    SendAllEvent:FireAllClients(text2,false,color)
end
--==================================
-- Sends a text that player has joined the game
game.Players.PlayerAdded:Connect(function(plr)
    local textsend = "[System]: "..plr.Name.." has joined the game!"
    SendAllEvent:FireAllClients(textsend,true)
end)
--===============================
-- sends message that player left the game
game.Players.ChildRemoved:Connect(function(plr)
    local textsend = "[System]: "..plr.Name.." has left the game!"
    SendAllEvent:FireAllClients(textsend,true)
end)

Event.OnServerEvent:Connect(NewMessageDisplay)

The server Script sends the message received from a client and sends it back to all clients. It also creates custom messages for the admins of the game. I need help with filtering the messages sent from the client to the server script. I want to add the function in the Server-Sided script. If I don't filter the message my game might get moderated. I am providing extra data so I can receive replies as soon as possible.

feel free to ask questions.

Your Average Roblox scripter,

ABDULGHANI1010

1 answer

Log in to vote
0
Answered by
Miniller 562 Moderation Voter
2 years ago
Edited 2 years ago

Read this article about text and chat filtering efficiently.

First you will have to use TextService:FilterStringAsync(message, fromPlayerID) to get an Instance of TextFilterResult.

You will then use textObject:GetNonChatStringForBroadcastAsync() to get the actual filtered string. (You could also use GetNonChatStringForUserAsync and GetChatForUserAsync for other purposes)

local TextService = game:GetService("TextService")

local function getTextObject(message, fromPlayerId)
    local textObject
    local success, errorMessage = pcall(function()
        textObject = TextService:FilterStringAsync(message, fromPlayerId)
    end)
    if success then
        return textObject
    end
    return false
end

local function getFilteredMessage(textObject)
    local filteredMessage
    local success, errorMessage = pcall(function()
        filteredMessage = textObject:GetNonChatStringForBroadcastAsync()
    end)
    if success then
        return filteredMessage
    end
    return false
end
--=========================================
-- if the name is in devs it makes devs specific message
function NewMessageDisplay(player, text)
    local messageObject = getTextObject(text, player.UserId)
    local filteredTextResult = ""
    if messageObject then 
        filteredTextResult = getFilteredMessage(messageObject)
        print(filteredTextResult)
    end

    if player.Name == "ABDULGHANI1010" or player.Name == "ImAGoodBoy12317" then
        color = DevTextMessageColor
        text2 = "[Dev]["..player.Name.."]: "..filteredTextResult
    else
        --if name is not in devs then makes a normal message
        color = NormalMessageColor
        text2 = "["..player.Name.."]: "..filteredTextResult
    end

    SendAllEvent:FireAllClients(text2,false,color)
end

Please note that this isn't the most efficient solution, when sending the same message multiple times the TextFilterResult will always be the exact same.

1
Thank you so much for the Help "Miniller" . It helped so much. We need more people like you and have a good Day/Night Hi_People1133 218 — 2y
Ad

Answer this question