Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

How would I go about changing the color of certain text in a textlabel like CSS?

Asked by
uhTeddy 101
5 years ago

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.

0
Hmm, roblox supplies api for text, for example you can get the size of an invididual letter. Maybe total the letters to size up the frame and position it accordingly. Not very experienced with GUIs. Formidable_Beast 197 — 5y

2 answers

Log in to vote
1
Answered by
brianush1 235 Moderation Voter
5 years ago

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:

local currentX = 0
local gui = Instance.new("ScreenGui", game.Players.LocalPlayer.PlayerGui) -- create a GUI

function createLabel(text, color)
    local label = Instance.new("TextLabel")

    label.TextStrokeTransparency = 0 -- set appearance properties
    label.TextColor3 = color
    label.Text = text
    label.TextSize = 18
    label.BackgroundTransparency = 1

    label.Position = UDim2.new(0.5, currentX, 0.5, 0) -- position this label to be in front of the other labels
    label.TextXAlignment = "Left" -- align to the left, since it's easier to work with
    label.Parent = gui -- set parent
    currentX = currentX + label.TextBounds.X -- move the next labels over by the width of this text
end

createLabel("[b_irdio]: ", Color3.new(1, 0, 0))
createLabel("Hello!", Color3.new(1, 1, 1))
Ad
Log in to vote
-1
Answered by 5 years ago

Every color variation is an additional TextLabel. TextLabels only define a single color.

Here is the core system's provision for customizing chat gui

0
I don't think that this is an answer Formidable_Beast 197 — 5y
0
tbh, would've been more appropriate as a comment theking48989987 2147 — 5y

Answer this question