Phone System Chat Filtering

Hello! So I’m trying to add filtering to a phone system, and I did a couple of searches on Google and good amount of YT videos, but with no luck, please help if you can. Thanks!

I’m attaching the local script that manages the message box, and the script that manages the filter string function. I will also attach my explorer tab to see the GUI layout.

Local Script

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local filterStringFunction = ReplicatedStorage:WaitForChild("FilterStringFunction")
local submitButton = script.Parent
local resultLabel = script.Parent.Parent.line0

script.Parent.MouseButton1Click:connect(function()
	local textEntered = script.Parent.Parent.MessageBox.Text
	local result, wasfiltered = filterStringFunction:InvokeServer(textEntered)
	resultLabel.Text = result
--script.Parent.Parent.values.newMessage.Value = script.Parent.Parent.Parent.thisPhone.Value .. ": " .. script.Parent.Parent.MessageBox.Text  --shows message on your end
		script.Parent.Parent.line10.Text = script.Parent.Parent.line9.Text
		script.Parent.Parent.line9.Text = script.Parent.Parent.line8.Text
		script.Parent.Parent.line8.Text = script.Parent.Parent.line7.Text
		script.Parent.Parent.line7.Text = script.Parent.Parent.line6.Text
		script.Parent.Parent.line6.Text = script.Parent.Parent.line5.Text
		script.Parent.Parent.line5.Text = script.Parent.Parent.line4.Text
		script.Parent.Parent.line4.Text = script.Parent.Parent.line3.Text
		script.Parent.Parent.line3.Text = script.Parent.Parent.line2.Text
		script.Parent.Parent.line2.Text = script.Parent.Parent.line1.Text
		script.Parent.Parent.line1.Text = script.Parent.Parent.line0.Text
		script.Parent.Parent.line0.Text = script.Parent.Parent.Parent.thisPhone.Value .. ": " .. script.Parent.Parent.MessageBox.Text
		print(script.Parent.Parent.line0.Text)
		print(script.Parent.Parent.MessageBox.Text)
	local workphone = script.Parent.Parent.Parent.otherPhone.Value--location of other phone
	local playerphone = workphone:FindFirstChild("player")--Finds player value inside other phone
	if playerphone.Value ~= nil then
		local pp = playerphone.Value --This is the Player(not character) location
		game.ReplicatedStorage.PhoneMessage:FireServer(pp,script.Parent.Parent.MessageBox.Text)
		--pp.PlayerGui.PhoneGUI.ChatScreen.values.newMessage.Value = script.Parent.Parent.Parent.thisPhone.Value .. ": " .. script.Parent.Parent.MessageBox.Text--Shows message on other end
	end
end)

Script

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

local filterStringFunction = ReplicatedStorage:WaitForChild("FilterStringFunction")

filterStringFunction.OnServerInvoke = function(player,textEntered)
	local filteredInstance = TextService:FilterStringAsync(textEntered,player.UserId,Enum.TextFilterContext.PublicChat)
	local filteredString = filteredInstance:GetNonChatStringForUserAsync(player.UserId)
	
end

1 Like

Hi, it seems like you’re doing the filtering itself correctly. However, keep in mind when using a RemoteFunction (Also make sure this is what you’re using, not a RemoteEvent), unlike RemoteEvents, you must return a value which will then be sent back to the client. In your case, you would do something like this:

filterStringFunction.OnServerInvoke = function(player,textEntered)
	local filteredInstance = TextService:FilterStringAsync(textEntered,player.UserId,Enum.TextFilterContext.PublicChat)
	local filteredString = filteredInstance:GetNonChatStringForUserAsync(player.UserId)
	
	return filteredString
end

You should also consider wrapping that in a pcall in order to ensure nothing in your script breaks.

Your local script would then recieve the value like so:

local filteredString = filterStringFunction:InvokeServer(textEntered)

You can return the “wasFiltered” value in the OnServerInvoke listener as well if you would like.

Lastly, while this isn’t relevant to what you needed help with, you might want to change those big repeating lines to something simple like this:

local chatScreen = script.Parent.Parent

for i = 10, 0, -1 do
	if i ~= 0 then
		chatScreen["line"..tostring(i)].Text = chatScreen["line"..tostring(i - 1)].Text
	else
		chatScreen.line0.Text = script.Parent.Parent.Parent.thisPhone.Value .. ": " .. script.Parent.Parent.MessageBox.Text --Just copy and pasted from your code, you'll probably want to clean this up too
	end
end
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.