文字過濾

*此內容是使用 AI(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
-- 用於發送用於過濾的文本輸入到伺服器的 RemoteEvent
local inputRemoteEvent = ReplicatedStorage:FindFirstChild("InputRemoteEvent")
-- 焦點丟失和按下 Enter 的事件處理程序
local function onFocusLost(enterPressed, inputObject)
if enterPressed then
print("已提交:", 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")
-- 用於接收來自客戶端的文本輸入以進行過濾的 RemoteEvent
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("生成 TextFilterResult 時出現錯誤:", errorMessage)
end
end
-- 當客戶端提交 TextBox 的輸入時觸發
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("已過濾:", filteredText)
else
warn("過濾文本時出現錯誤!")
end
end
end
end
inputRemoteEvent.OnServerEvent:Connect(onInputReceived)
©2026 Roblox Corporation、Roblox、Roblox 標誌及 Powering Imagination 是我們在美國及其他國家地區的部分註冊與未註冊商標。