Hey everyone! I hope you are having a great day so far! I started making a little game for fun, which I won’t go in depth about because you probably know why, but in my game, the player can type in a text box, and I was wondering how I can filter this text. I want this because obviously I can’t have players typing who knows what, but right now you can type anything and it will work.
Here is a video demonstrating-
If anyone has anything that can help me, I would really appreciate it, and I am sort of new to scripting, so if you could explain it, that would be really helpful, but not necessary! Please do not mention the ui, this is just testing!
You can use this function to filter text, but you need to do it on the server.
local TextService = game:GetService("TextService")
function FilterInput(message: string, userId: number): string?
if typeof(message) ~= "string" or message == "" then
return nil
end
local success, filteredText = pcall(function()
local filterResult = TextService:FilterStringAsync(message, userId)
return filterResult:GetNonChatStringForBroadcastAsync()
end)
if success and filteredText then
return filteredText
else
warn("Text filtering failed:", filteredText)
return nil
end
end
As I currently can’t use Roblox Studio I can’t help you further if you have any issues.
local TextService = game:GetService("TextService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Your filter function from before
function FilterInput(message: string, userId: number): string?
if typeof(message) ~= "string" or message == "" then
return nil
end
local success, filteredText = pcall(function()
local filterResult = TextService:FilterStringAsync(message, userId)
return filterResult:GetNonChatStringForBroadcastAsync()
end)
if success and filteredText then
return filteredText
else
warn("Text filtering failed:", filteredText)
return nil
end
end
-- Create RemoteFunction for clients to call
local remoteFunction = Instance.new("RemoteFunction")
remoteFunction.Name = "RequestFilter"
remoteFunction.Parent = ReplicatedStorage
-- Handle client requests
remoteFunction.OnServerInvoke = function(player, message)
return FilterInput(message, player.UserId)
end
Local script:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RequestFilter = ReplicatedStorage:WaitForChild("RequestFilter")
--[[
FilterTextOnClient(message: string): string?
Sends text to the server for filtering.
Returns the filtered text (safe to display), or nil if filtering failed.
]]
local function FilterTextOnClient(message: string): string?
if typeof(message) ~= "string" or message == "" then
warn("FilterTextOnClient: Invalid message")
return nil
end
local success, result = pcall(function()
return RequestFilter:InvokeServer(message)
end)
if success and result then
return result
else
warn("FilterTextOnClient failed:", result)
return nil
end
end
Note that this script may contain bugs, and I’m not responsible for consequenses of using this script. I am still positive that this script does not contain bugs and is aligned with Roblox’s requirements.