文字過濾

*此內容是使用 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
-- 遠端事件將文字輸入傳送給服務器進行過濾
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)