-- Applying text filter

local function getTextObject(message, fromPlayerId)
	local textObject
	local success, errorMessage = pcall(function()
		textObject = TextService:FilterStringAsync(message, fromPlayerId, Enum.TextFilterContext.PublicChat)
	end)
	if success then
		return textObject
	elseif errorMessage then
		print("Error filtering message:", errorMessage)
	end
	return false
end

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

local function filterText(text, fromPlayerId, toPlayerId)
	local textObject = getTextObject(text, fromPlayerId)
	if textObject then
		local filteredObject = getFilteredMessage(textObject, toPlayerId)
		return filteredObject
	end
	return false
end
