How do I put instances in strings?

So I am using richtext to color a players name in chat, and I want to put the player’s name into a string.


local function rgb255RichText(color3)
	local R = (color3.R * 255)
	local G = (color3.G * 255)
	local B = (color3.B * 255)

	return string.format("rgb(%d, %d, %d)", R, G, B)
end

function createtext(stri,plr)
	local c = Text:Clone()
	local color = rgb255RichText(Color3.fromRGB(0, 55, 235))
	
	local name =  color
	
	c.Text = [[<font color="]] .. color .. [[">[ "PLAYER NAME HERE" ]</font>]] .. " " .. stri

	c.Parent = goal
end

Can someone tell me how I would put Player.Name where it says “PLAYER NAME HERE”?

c.Text = `<font color="rgb(0,55,235)">[ {plr.Name} ]</font> {stri}`

edited: fixed rich text syntax

2 Likes

this doesn’t work, and it doesn’t account for multiple colors either

sorry missing the double quotes for the color argument

c.Text = `<font color="rgb(0,55,235)">[ {plr.Name} ]</font> {stri}`
1 Like

it works really good! but this line right here

`<font color="rgb(0,55,235)">

still only accounts for one color.

I have a function that makes a new color for every player. Is it possible for it to accept a color3.rgb and not a string?
image

local color = Color3.fromRGB(0, 55, 235)
c.Text = `<font color="{rgb255RichText(color)}">[ {plr.Name} ]</font> {stri}`

basically, within the backquotes `` we can use { } to include code and variables

2 Likes

The Color3 data-type has a ToHex method, which you can use to set the font color:

-- There's no longer a need to use the rgb255RichText function
function createtext(stri, plr)
	local color = Color3.fromRGB(0, 55, 235) -- Create a Color3 value using the RGB values of your choosing

	local c = Text:Clone()
	c.Text = `<font color="#{color:ToHex()}">[{plr.Name}]:</font> {stri}` -- Convert the color to hex, and format the text
	c.Parent = goal
end
1 Like