To start off. I'm NOT in studio. I tried to use this code in a local script, It's in the workspace. Maybe that's the problem: Here's the code so you can look at it!

-- LocalScript
 
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
 
local player = Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local screen = playerGui:WaitForChild("MessageScreen")
local sendMessageEvent = ReplicatedStorage:WaitForChild("SendPrivateMessage")
 
-- GUI elements for send frame
local sendFrame = screen:WaitForChild("SendFrame")
local recipientField = sendFrame:WaitForChild("Recipient")
local writeMessageField = sendFrame:WaitForChild("Message")
local sendButton = sendFrame:WaitForChild("Send")
 
-- GUI elements for receive frame
local receiveFrame = screen:WaitForChild("ReceiveFrame")
local senderField = receiveFrame:WaitForChild("From")
local readMessageField = receiveFrame:WaitForChild("Message")
 
-- Called when send button is clicked
local function onSendClicked()
	-- Try to find the recipient. Only want to send message if recipient exists
	local recipient = Players:FindFirstChild(recipientField.Text)
	local message = writeMessageField.Text
	if recipient and message ~= "" then
		-- Send the message
		sendMessageEvent:FireServer(recipient, message)
		-- Clean up send frame
		recipientField.Text = ""
		writeMessageField.Text = ""
	end	
end
 
-- Called when send message event fires meaning this client got a message
local function onReceiveMessage(sender, message)
	-- Populate fields of receive frame with the sender and message
	senderField.Text = sender.Name
	readMessageField.Text = message
end
 
-- Bind event functions
sendButton.MouseButton1Click:Connect(onSendClicked)
sendMessageEvent.OnClientEvent:Connect(onReceiveMessage)

-- Server Script
 
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TextService = game:GetService("TextService")
 
local sendMessageEvent = ReplicatedStorage.SendPrivateMessage
 
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, toPlayerId)
	local filteredMessage
	local success, errorMessage = pcall(function()
		filteredMessage = textObject:GetChatForUserAsync(toPlayerId)
	end)
	if success then
		return filteredMessage
	end
	return false
end
 
-- Called when client sends a message
local function onSendMessage(sender, recipient, message)
	if message ~= "" then
		-- Filter the incoming message and send the filtered message
		local messageObject = getTextObject(message, sender.UserId)
 
		if messageObject then
			local filteredMessage = getFilteredMessage(messageObject, recipient.UserId)
			sendMessageEvent:FireClient(recipient, sender, message)
		end
	end
end
 
sendMessageEvent.OnServerEvent:Connect(onSendMessage)

2 comments

0

Have you tried testing it with two people? Maybe it will censor it.

0

You don't have access to the filter API locally, invoke a remote function and filter it server side.