文本筛选

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处

应用于各种来源和输入, 文本过滤 防止用户看到不当的语言和可识别的个人信息,例如电话号码。Roblox 自动过滤通过 体验文本聊天 的常见文本输出,例如消息,但 你对任何显示的文本负责过滤你没有明确控制权的任何文本

过滤场景

文本可以在各种情况下收集和/或显示给用户,包括:

  • 收集用户的 文本输入 通过 TextBox 条目的经验,一个带有按钮的自定义图形用户界面 (GUI),或在 3D 空间中的互动键盘模型。

  • 一个从随机字符生成词并将其显示给用户的体验,因为它有可能创建不当的词。

  • 连接到外部网服务器以获取体验中显示的内容的经验。经常你不会对外部网站的内容有控制权,第三方可以编辑信息。

  • 一个使用 数据存储库 存储用户的宠物名称等文本的体验,其存储的文本可能包含不当词语,在检索时应被过滤。

筛选过程

TextService:FilterStringAsync() 在经验中过滤文本,通过采取一串文本和创建文本的用户的 UserId 作为输入。它返回一个 TextFilterResult 对象,该对象具有两个额外的方法,您可以在不同的场景中调用:

TextBox 输入的上下文中,以下示例收集了关于 FocusLost 事件的输入,并将其通过 RemoteEvent 发送到服务器。在服务器上,首先通过 FilterStringAsync() 被过滤,然后通过 GetNonChatStringForBroadcastAsync() 将文本显示给服务器端对象,例如 3D 世界中的 SurfaceGui

过滤文本输入 - 客户端脚本

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local textBox = script.Parent
textBox.ClearTextOnFocus = false
textBox.PlaceholderText = "..."
textBox.TextXAlignment = Enum.TextXAlignment.Left
textBox.TextScaled = true
-- 远程事件将文本输入发送到服务器进行筛选
local inputRemoteEvent = ReplicatedStorage:FindFirstChild("InputRemoteEvent")
-- 焦点丢失和输入被按下的事件处理器
local function onFocusLost(enterPressed, inputObject)
if enterPressed then
print("SUBMITTED:", textBox.Text)
if inputRemoteEvent then
inputRemoteEvent:FireServer(textBox.Text)
end
end
end
textBox.FocusLost:Connect(onFocusLost)
过滤文本输入 - 服务器脚本

local TextService = game:GetService("TextService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- 远程事件接收客户端的文本输入以进行筛选
local inputRemoteEvent = ReplicatedStorage:FindFirstChild("InputRemoteEvent")
local function getFilterResult(text, fromUserId)
local filterResult
local success, errorMessage = pcall(function()
filterResult = TextService:FilterStringAsync(text, fromUserId)
end)
if success then
return filterResult
else
warn("Error generating TextFilterResult:", errorMessage)
end
end
-- 当客户端从文本框提交输入时发射
local function onInputReceived(player, text)
if text ~= "" then
local filterResult = getFilterResult(text, player.UserId)
if filterResult then
local success, filteredText = pcall(function()
return filterResult:GetNonChatStringForBroadcastAsync()
end)
if success then
print("FILTERED:", filteredText)
else
warn("Error filtering text!")
end
end
end
end
inputRemoteEvent.OnServerEvent:Connect(onInputReceived)