How could I make this script filter what the user types?

Hey, I just finished this script that lets users edit the text on a sign although you can type inappropriate text without it being filtered. Any help with this script is appreciated.

Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local EditEvent = ReplicatedStorage.Events:FindFirstChild("EditEvent")

local Sign = game.Workspace:FindFirstChild("Sign")
local SignText = Sign.Structure.Signs.TextSign.SurfaceGui:FindFirstChild("Text")
local EditPrompt = Sign.Structure.Prompt:FindFirstChild("Edit")

local EditFrame = Players.LocalPlayer.PlayerGui.Edit:FindFirstChild("Frame")

local Close = EditFrame.Buttons:FindFirstChild("Close")
local Apply = EditFrame.Buttons:FindFirstChild("Apply")
local Text = EditFrame.Buttons:FindFirstChild("Text")

EditEvent.OnClientEvent:Connect(function()
	EditFrame:TweenPosition(UDim2.new(0.5, 0, 0.5, 0), "Out", "Sine", 0.5, true)
end)

Close.MouseButton1Down:Connect(function()
	EditFrame:TweenPosition(UDim2.new(0.5, 0, -1, 0), "Out", "Sine", 0.5, true)
	EditPrompt.Enabled = true
end)

Apply.MouseButton1Down:Connect(function()
	SignText.Text = Text.Text
	EditFrame:TweenPosition(UDim2.new(0.5, 0, -1, 0), "Out", "Sine", 0.5, true)
	EditPrompt.Enabled = true
end)

TextService:FilterStringAsync(text, fromPlayerId)

Thanks for this information but could you clarify where this would go in the script?

If you intend for the sign to be viewed as well for other players, string filtering needs to occur server-sided so when the server transmits data to the other clients to replicate the sign text, it’s secure.

Otherwise exploiters can send through any text that they want since text filtering is only done locally, explicit text can be seen by other users if it replicates to the server.

Could you clarify on how I could implement this? I’m relatively new to scripting.

So when you fire your RemoteEvent to the server to replicate (or make known the changes of) the text for other players to see, that’s when you want to apply text filtering to the text specified by the user. Then the server will send the filtered text over to all other clients so they can see what the sign was edited to say.

Some pseudocode:

--//Client invokes RemoteEvent locally, server receives the call
local chatService = game:GetService("Chat")

signRemote.OnServerEvent:Connect(function(player, signText)
--//Filter the string specified by the user, then replicate it to other players
if type(signText) ~= "string" then warn("signText argument not a string") end

local filteredText;
pcall(function() filteredText = chatService:FilterStringForBroadcast(signText, player)
end)

if filteredText then
signRemote:FireAllClients(player, filteredText)
end

end)

--//And then on the client, set up a function that'll execute when they receive the request to update the sign.

signRemote.OnClientEvent:Connect(function(playerWhoEditedSign, signText)
--//do stuff with this. you can skip doing anything if playerWhoEditedSign is the LocalPlayer
if playerWhoEditedSign == game.Players.LocalPlayer then return end

end)

Okay so with this what would I do with the original local script located in StarterCharacterScripts to accommodate with this. Since currently this function inside of it is what updates the text.

Apply.MouseButton1Down:Connect(function()
	SignText.Text = Text.Text
	EditFrame:TweenPosition(UDim2.new(0.5, 0, -1, 0), "Out", "Sine", 0.5, true)
	EditPrompt.Enabled = true
end)

I think you’d put that in where the client receives the request to update the sign.

signRemote.OnClientEvent:Connect(function(playerWhoEditedSign, signText)
    SignText.Text = signText
	EditFrame:TweenPosition(UDim2.new(0.5, 0, -1, 0), "Out", "Sine", 0.5, true)
	EditPrompt.Enabled = true
end)

Alright so with all the edits when I try updating the text this shows in the output

Send Text.Text as an argument when you fire the RemoteEvent.

signRemote:FireServer(Text.Text)

Alright so I’ve applied this although this error shows, but I’m thinking I’ve messed something up within the local script. Here are the last 2 functions, could you clarify if it’s proper? If you want I could temporarily share the game with you by the way.

Apply.MouseButton1Down:Connect(function()
	UpdateText:FireServer(Text.Text)
end)

UpdateText.OnClientEvent:Connect(function(SignEditor, SignText)
	SignText.Text = SignText
	EditFrame:TweenPosition(UDim2.new(0.5, 0, -1, 0), "Out", "Sine", 0.5, true)
	EditPrompt.Enabled = true
end)