SmartText2 | Module for Automatic RichText Formatting

Hello there!


A while back, I had made an open-sourced module called SmartText that could help in formatting text with Roblox’s RichText feature. And although it was kinda useful, it could’ve been a whole lot better. That’s why I decided to make SmartText2! The biggest improvement with this module is the ability to chain its functions. An example could look like this:

SmartText.new("Hello, world!"):Bold():Underline():ColorRGB(255,255,255)

This makes the module much more convenient, and keeps your scripts looking cleaner.

What exactly is SmartText2?


Like previously stated, SmartText2 is a ModuleScript that makes it easy to format your text with Roblox’s RichText feature. The module can create different text “objects”, which comes with various methods for formatting that text. And what makes this module so special is that these methods can be chained, making your code cleaner and more efficient.

How do I get started?


Get started with SmartText2 in three simple steps:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SmartText2 = require(ReplicatedStorage:WaitForChild("SmartText2"))

You can also require the module using it’s AssetId (won’t work in LocalScripts):

local SmartText2 = require(6598530777)

How do I use SmartText2?


SmartText2’s documentation can be found here

With SmartText2 required from your script, we can begin to use it! So lets start by creating a SmartText2 text object:

local text = SmartText2.new()

This will create a new SmartText2 object. By default, the text will be an empty string. But if you would like, you can pass in your own string in the text parameter of the constructor:

local text = SmartText2.new("This is some text!")

Now this text object actually has some text, but lets say you don’t want to add any text right away and/or you want to add some text later on. In that case, you can use this object’s :Concat() method. This method will concatenate the object’s current text with some more text.

local text = SmartText2.new("This is some text!")
text:Concat(" More text!") --> This is some text! More text!

But like I mentioned earlier, these methods can be chained. So the above example could also be written as:

local text = SmartText2.new("This is some text!"):Concat(" More text!") --> This is some text! More text!

There are, of course, more methods besides the :Concat() function. You can find the entire list on SmartText2’s documentation.

So lets actually start formatting your text! To bolden your text, we can use the :Bold() method:

local text = SmartText2.new("Text"):Bold() --> <b>Text</b>

Now your text is formatted to look bold! What’s cool about this method, as well as most of the other methods, is that you can concatenate the formatted text, rather than formatting the entire thing. If you pass a string through the :Bold() method, the result would look like this:

local text = SmartText2.new("This is"):Concat():Bold("bold text") --> This is <b>bold text</b>
-- using Concat() with no arguments will default to a space (' ')

That works the same way with most of the other methods of the SmartText2 object.

Some methods, like the :Size() method, contain more than one parameter for arguments to be passed through it. For example, the :Size() method has an optional text parameter and a required size parameter:

local text = SmartText2.new("Big text"):Size(50) --> <font size="50">Big text</font>

This, of course, has that optional text parameter that can concatenate a string with the format, rather than formatting the entire thing:

local text = SmartText2.new("This is"):Concat():Size("big text", 50) --> This is <font size="50">big text</font>

So we can create a text object and format it to our heart’s desire, but how do we actually use it in scripting? The SmartText2 text object is actually a table, so to access the object’s text, we can index it with .text, which will return the text. You can also pass it through tostring(), which essentially does the same thing. For example:

local text = SmartText2.new("This is some awesome text!"):Bold():Size(50)

print(text.text) --> <font size="50"><b>This is some awesome text!</b></font>
-- or
print(tostring(text)) --> <font size="50"><b>This is some awesome text!</b></font>

The SmartText2 class utilizes the .__concat metamethod, so you can safely concatenate a SmartText2 text object with any string, as well as two different SmartText2 objects, which will combine them both into a new text object:

local text1 = SmartText2.new("Hello")
local text2 = text1 .. ", world!" -- returns a string

print(text2) --> Hello, world!
local text1 = SmartText2.new("Hello"):Bold():Concat(", ")
local text2 = SmartText2.new("world!"):Size(50)
local text3 = text1 .. text2 -- returns a new text object

print(text3.text) --> <b>Hello</b>, </font size="50">world!</font>

Notice how, when I concatenate a SmartText2 text object with a string, it returns a string; not a text object. As for concatenating two different SmartText2 text objects, it combines them and returns an entirely new text object.

There’s so much more you can do with this module. If you want to learn more, you can check out SmartText2’s documentation here.

Examples


Bellow are a few examples I made using this module. All examples are a LocalScript, which are parented to a TextLabel:

image

Example #1

Script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SmartText2 = require(ReplicatedStorage:WaitForChild("SmartText2"))

local text1 = SmartText2.new("SmartText2"):Bold():Size(30):Face("Kalam"):ColorRGB(100,200,200)
local text2 = SmartText2.new("automatic formatting"):Bold():ColorRGB(100,200,200)

script.Parent.Text = "By using " .. text1 .. ", you can get amazing-looking text with " .. text2

Result

Example #2

Script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SmartText2 = require(ReplicatedStorage:WaitForChild("SmartText2"))

local text = SmartText2.new()

local function Countdown(duration)
	for i = duration, 0, -1 do
		text:Set("Time left: "):ColorRGB(i, 200,50,50):Bold():Size(50)
		script.Parent.Text = text.text
		
		wait(1)
	end
end

Countdown(60)

Result

Thank you!


Thank you for taking the time to check out my SmartText2 module! If you want to learn more about this module, make sure you check out the documentation.

24 Likes

Wow, this module is absolutely useful! Keep it up :sunglasses:

1 Like

I would use this for text formatting (for my custom chat) and I think I would use this, tysm!

1 Like

Uh, why text:Concat() and not text…" More text!"?
Using meta tables you can get the concat function and easily use it.

If you read the post, he already is. He just added that function as an alternative.

1 Like