In many cases I see games using text label(s) that include various colors. One I see mostly that is inplimented into every game by default is Roblox's Chat system. In the system it has your username in one color and then your message in another. I made a chat system before using different colors but, I used Defaultio's Markup Module. He creates text labels individualy used for each letter. I want to be able to have say [b_irdio]: as one label and then Hello! as another label.
It's probably better to use a module, since there's no point in reinventing the wheel (unless you're making a better wheel)
But alas, here's the simplest way to do it:
01 | local currentX = 0 |
02 | local gui = Instance.new( "ScreenGui" , game.Players.LocalPlayer.PlayerGui) -- create a GUI |
03 |
04 | function createLabel(text, color) |
05 | local label = Instance.new( "TextLabel" ) |
06 |
07 | label.TextStrokeTransparency = 0 -- set appearance properties |
08 | label.TextColor 3 = color |
09 | label.Text = text |
10 | label.TextSize = 18 |
11 | label.BackgroundTransparency = 1 |
12 |
13 | label.Position = UDim 2. new( 0.5 , currentX, 0.5 , 0 ) -- position this label to be in front of the other labels |
14 | label.TextXAlignment = "Left" -- align to the left, since it's easier to work with |
15 | label.Parent = gui -- set parent |
16 | currentX = currentX + label.TextBounds.X -- move the next labels over by the width of this text |
17 | end |
18 |
19 | createLabel( "[b_irdio]: " , Color 3. new( 1 , 0 , 0 )) |
20 | createLabel( "Hello!" , Color 3. new( 1 , 1 , 1 )) |
Every color variation is an additional TextLabel. TextLabels only define a single color.
Here is the core system's provision for customizing chat gui