How can I block certain messages on the server

With the old legacy Chat, I could block certain player messages by doing:

local function Run(ChatService)

	local function BlockWords(speakerName, message, channelName)
		
			if excludePlayer(PlayerName) then 
				return true --indicates to stop processing the chat message
			end
			return false --word is not blocked, continue chat processing
		end

		ChatService:RegisterProcessCommandsFunction("BlockWords", BlockWords)
end

This would be a child module under Chat.

This server method no longer works with the new TextChatService.

The only new API I found that could do this is: TextChatService.OnIncomingMessage which only runs on the client.

I could still use this API but I would need to place this listener on every client, then for every single chat message, I would need to invoke the server and see if that message can be displayed then let the message through or block it on the client. Though possible, this is really inefficient and there must be a better way I don’t know of.

nvm I figured it out, solution for future wanderers updating their chat API:

local defaultChannel = TextChatService:WaitForChild("TextChannels"):WaitForChild("RBXGeneral")
defaultChannel.ShouldDeliverCallback = function(message: TextChatMessage, textSource: TextSource)
	print("Filtering: ".. message.Text)
	
	if string.split(message.Text, "/")[2] then
		return false
	end
	return true
end

I saw this callback earlier but I assumed it was a client only when it said ’ Called for each client when TextChannel is receiving an incoming message to determine whether or not it should be delivered to that client.’ I completely glossed over Can only be defined on the server. :man_facepalming:

I know this is a very dumb question but, where exactly would this script go?

On the server. The ShouldDeliverCallback can only be used from the server.

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