TextFilterResult

Show Deprecated
not creatable
not replicated

Represents the result of a call to TextService:FilterStringAsync(). Used to distribute a filtered string accordingly.

Summary

Methods

Properties

Methods

GetChatForUserAsync

yields

The GetChatForUserAsync function returns the text in a properly filtered manner for the specified Player.UserId. This should be used in the context of chats between players, although there are some other cases where text filtering is required.

This function returns the string appropriate for sending and displaying to a target user (specified by toUserId) from the original sender using the least restrictive filtering appropriate for the target user, with Chat privacy settings of both users enforced. This string should only be shown to the target user, as it might not be appropriate for all users.

This method throws an error if the two users are not allowed to chat (that is, if Chat:CanUserChatAsync() would return false for the given sender and receiver). If this method throws the string should not be displayed to the user. In addition, this function will throw an error if CanUserChatAsync would return false, so CanUserChatAsync should be called first to check.

This function currently throws an error if the user with the id toUserId is not online on the current server.

If text can be used for real-time or near real-time communication it should use this method.

This function will return immediately in most cases. The only time it will yield is if the target user is offline or has just joined the server and their filtering info is not yet loaded.

Parameters

toUserId: number

ID of the user being chatted.


Returns

Filtered text string.

Code Samples

TextFilterResult:GetChatForUserAsync (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
else
warn(errorMessage)
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
else
warn(errorMessage)
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, filteredMessage)
end
end
end
sendMessageEvent.OnServerEvent:Connect(onSendMessage)
TextFilterResult:GetChatForUserAsync (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")
local sendFrame = screen:WaitForChild("SendFrame")
local recipientField = sendFrame:WaitForChild("Recipient")
local writeMessageField = sendFrame:WaitForChild("Message")
local sendButton = sendFrame:WaitForChild("Send")
local receiveFrame = screen:WaitForChild("ReceiveFrame")
local senderField = receiveFrame:WaitForChild("From")
local readMessageField = receiveFrame:WaitForChild("Message")
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
sendMessageEvent:FireServer(recipient, message)
-- Clean up send frame
recipientField.Text = ""
writeMessageField.Text = ""
end
end
local function onReceiveMessage(sender, message)
-- Populate fields of receive frame with the sender and message
senderField.Text = sender.Name
readMessageField.Text = message
end
sendButton.MouseButton1Click:Connect(onSendClicked)
sendMessageEvent.OnClientEvent:Connect(onReceiveMessage)

GetNonChatStringForBroadcastAsync

yields

Returns the text in a properly filtered manner for all users.


Returns

GetNonChatStringForUserAsync

yields

Returns the text in a properly filtered manner for the specified Player.UserId. This should be used in the context of non-chat text that another user can see, such as the name of a pet.

Parameters

toUserId: number

Returns

Events