さまざまなソースや入力に適用されるテキストフィルタリングは、ユーザーが不適切な言語や電話番号などの個人を特定できる情報を見ないようにします。Robloxは、ゲーム内テキストチャットを通過したメッセージなど、一般的なテキスト出力を自動的にフィルタリングしますが、明示的に制御できない表示テキストのフィルタリングはあなたの責任です。
フィルタリングシナリオ
テキストは、さまざまなシナリオでユーザーに収集および/または表示される可能性があります。これには以下が含まれます:
ランダムな文字から単語を生成し、それをユーザーに表示するゲーム。これは不適切な単語を生成する可能性があります。
外部Webサーバーに接続して、ゲーム内に表示されるコンテンツを取得するゲーム。外部サイトのコンテンツを制御できないことが多く、第三者が情報を編集できる可能性があります。
データストアを使用してユーザーのペットの名前などのテキストを保存するゲーム。保存されたテキストにはフィルタリングすべき不適切な単語が含まれている可能性があります。
フィルタリングプロセス
TextService:FilterStringAsync()は、テキストの文字列とそのテキストを作成したユーザーのUserIdを入力として受け取り、ゲーム内のテキストをフィルタリングします。これは、異なるシナリオで呼び出すことができる2つの追加メソッドを持つTextFilterResultオブジェクトを返します:
- TextFilterResult:GetNonChatStringForBroadcastAsync()は、サーバー上のすべてのユーザーに表示されるテキストをフィルタリングするために使用されます。たとえば、すべてのユーザーに見えるサインにメッセージを書くためのダイアログなどです。
- TextFilterResult:GetNonChatStringForUserAsync()は、年齢やその他の詳細に基づいて特定のユーザーにフィルタリングされたテキストを表示します。
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)