Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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.

1 comment

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.

2 answers

✓ Accepted answer

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))

This discussion is preserved as it was. New answers, comments, and votes are closed.