文字過濾

*此內容是使用 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 是我們在美國及其他國家地區的部分註冊與未註冊商標。