Server filtering

I have this code, that used to work. I added filtering, so it’s safe. I think everything should work there, but I get a nil. I tried words that I know will get blocked, and words that won’t. Nothing is in the console.

Server-side

local event = game.ReplicatedStorage:WaitForChild("Intercom")


event.OnServerEvent:Connect(function(p, text)
	
	local TextService = game:GetService("TextService")

		local filteredText = ""
	local success, errorMessage = pcall(function()
		filteredText = TextService:FilterStringAsync(text, p.UserId)
	end)
	if not success then
		warn("Error filtering text")
		-- Put code here to handle filter failure
	end
	
	event:FireAllClients(filteredText)


end)

i am a bit stupid

This text will be blurred

honestly idrk im not that well-versed in textservice, sorry.

I fixed the empty text, with the new code (it’s in the question). Now i get nil

Replace the string assignment with a return call so the ErrorMessage can be either the result or an error message.

Try printing out the error and see what happens

return TextService:FilterStringAsync() —[[obviously put parameters inside the function call]]

errorMessage is nil. Not sure why.

Where did you print it out? The client or server?

Oh also try printing out the text received from the remote event on its on server event call

Not sure if I did what you wanted, but I did print()

That is all correct. Prints out the correct text.

I’m not sure why it isn’t working, really. The return nil, meaning the call itself might be failing, though you did set up some sort of error detection.

Maybe check out the creator documentation for TextService and see if you can figure it out yourself?

I now have this code i copied from Roblox dev docs. Now it shows the text, but doesn’t filter it.

local event = game.ReplicatedStorage:WaitForChild("Intercom")
local TextService = game:GetService("TextService")

local function getTextObject(message, fromPlayerId)
	local textObject
	local success, errorMessage = pcall(function()
		textObject = TextService:FilterStringAsync(message, fromPlayerId)
	end)
	if success then
		return textObject
	elseif errorMessage then
		print("Error generating TextFilterResult:", errorMessage)
	end
	return false
end

local function getFilteredMessage(textObject)
	local filteredMessage
	local success, errorMessage = pcall(function()
		filteredMessage = textObject:GetNonChatStringForBroadcastAsync()
	end)
	if success then
		return filteredMessage
	elseif errorMessage then
		print("Error filtering message:", errorMessage)
	end
	return false
end

-- Fired when client sends a request to write on the sign
local function onSetSignText(player, text)
	if text ~= "" then
		-- Filter the incoming message and send the filtered message
		local messageObject = getTextObject(text, player.UserId)
		local filteredText = ""
		filteredText = getFilteredMessage(messageObject)
		event:FireAllClients(filteredText)
	end
end

event.OnServerEvent:Connect(function(p, text)
	onSetSignText(p, text)
end)```

I’m just blindly firing here, but could it be broken because of the recent age restrictions added to games? Try going through the form to set it up.

Could be so, but it literally doesn’t filter swear words. ones that wouldn’t be allowed to 13+ too.

1 Like

I am not eligible for Experience Questionnaire yet.

I might’ve figured it out. Filtering doesn’t work in Studio, you have to test it in experience.

local event = game.ReplicatedStorage:WaitForChild("Intercom")
local TextService = game:GetService("TextService")

local function getTextObject(message, fromPlayerId)
	local textObject
	local success, errorMessage = pcall(function()
		textObject = TextService:FilterStringAsync(message, fromPlayerId)
	end)
	if success then
		return textObject
	elseif errorMessage then
		print("Error generating TextFilterResult:", errorMessage)
	end
	return false
end

local function getFilteredMessage(textObject)
	local filteredMessage
	local success, errorMessage = pcall(function()
		filteredMessage = textObject:GetNonChatStringForBroadcastAsync()
	end)
	if success then
		return filteredMessage
	elseif errorMessage then
		print("Error filtering message:", errorMessage)
	end
	return false
end

-- Fired when client sends a request to write on the sign
local function onSetSignText(player, text)
	if text ~= "" then
		-- Filter the incoming message and send the filtered message
		local messageObject = getTextObject(text, player.UserId)
		local filteredText = ""
		filteredText = getFilteredMessage(messageObject)
		event:FireAllClients(filteredText)
	end
end

event.OnServerEvent:Connect(function(p, text)
	onSetSignText(p, text)
end)```
1 Like

you forgot to put return filteredText in the function.

That’s good to know, explains why it didn’t work last time I used it.

there’s no need to return it, its being fired to all clients

I meant in the pcall function.

Thanks, I figured it out! You have to test it in experience. :grinning: :grin:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.