Filtering messages returns nil?

Im trying to make a custom chat and when I filter the messages on a server script, it sends nil?

The text printed will give me the text inputted
But the filtertextresult printed gives me nil?

local TextChatService = game:GetService("TextService")
game.ReplicatedStorage.ChatMessageSend.OnServerEvent:Connect(function(player, text)
    local FilterTextResult = ""


    local success, errorMessage = pcall(function()
        FilterTextResult = TextChatService:FilterStringAsync(text, player.UserId)
    end)
    
    task.wait(0.05)
    if success then
        game.ReplicatedStorage.ChatMessageEvent:FireAllClients(player, FilterTextResult)
    else
        warn(errorMessage)
    end
end)

FilterStringAsync returns a TextFilterResult

You can use GetChatForUserAsync method to get the filtered message

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
	else
		warn(errorMessage)
	end
	return false
end

local function getFilteredMessage(textObject, toPlayerId)
	local filteredMessage
	local success, errorMessage = pcall(function()
		filteredMessage = textObject:GetChatForUserAsync(toPlayerId)
	end)
	if success then
		return filteredMessage
	else
		warn(errorMessage)
	end
	return false
end

game.ReplicatedStorage.ChatMessageSend.OnServerEvent:Connect(function(player, text)
	if text == "" then
        return
    end

    local messageObject = getTextObject(text, player.UserId)
    if not messageObject then
        return
    end

    local filteredMessage = getFilteredMessage(messageObject, player.UserId)
    game.ReplicatedStorage.ChatMessageEvent:FireAllClients(player, filteredMessage)
end)
2 Likes

I tried this, the chat isnt being filtered i can say any profanity

Filtering doesn’t work inside of studio, you have to Team Test or test inside game.

1 Like

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