Textbox keywords in different colors

So I have a TextBox and When I type in it, it is green, like it is supposed to be, but I want it so everytime I type the word “local”, only the word local changes colors. and the saem with “print” but that turns blue
so if I had hello local print hi print it would be green red blue green blue, if you understand

4 Likes

I’m afraid that’s not possible in Roblox (yet).

what about rich text? i know how to use it, but not in the way i want

Ah I see what richtext does. However, it seems to only change one type of the string used in the text.

There’s a few posts on the forum for stuff like this. This one may work for you:

If you want different words to have different colors, you’d need to modify the script a little

that only works when i stop focusing on the textbox, and doesnt work when i put like h script h only when its just script

This is absolutely possible on roblox, and wouldn’t be that complicated.

do you know how? because ive looked everywhere

bceause this works:


but only when i stop focusing on the text

yep im working on a solution right now, give me some time.

function convertString(stringToConvert: string, colors)
	local newString = stringToConvert
	for keyword, newColor : Color3 in pairs(colors) do
		newString = string.gsub(newString, keyword, '<font color="rgb('.. tostring(math.floor(newColor.R * 255)).. ','.. tostring(math.floor(newColor.G * 255)).. ','.. tostring(math.floor(newColor.B * 255)).. ')">'.. keyword.. '</font>')
	end
	
	return newString
end

local stringToConvert = 'local variable = nil print("hello")'
local colors = {
	['local'] = Color3.fromRGB(204, 0, 0),
	['print'] = Color3.fromRGB(0, 229, 255)
}


local newString = convertString(stringToConvert, colors)

print(newString)

This code will print the new formatted string. You can specify what words you want to be replaced with what colors in the colors table.

1 Like

how would i make it so it does that everytime the textbox text changes

just link it with the GetPropertyChangedSignal(“Text”)

like this?

function convertString(stringToConvert: string, colors)
	local newString = stringToConvert
	for keyword, newColor : Color3 in pairs(colors) do
		newString = string.gsub(newString, keyword, '<font color="rgb('.. tostring(math.floor(newColor.R * 255)).. ','.. tostring(math.floor(newColor.G * 255)).. ','.. tostring(math.floor(newColor.B * 255)).. ')">'.. keyword.. '</font>')
	end

	return newString
end

local colors = {
	['local'] = Color3.fromRGB(204, 0, 0),
	['print'] = Color3.fromRGB(0, 229, 255)
}
local stringToConvert = script.Parent.Text



local newString = convertString(stringToConvert, colors)

script.Parent:GetPropertyChangedSignal("Text"):Connect(function()
	local stringToConvert = script.Parent.Text
	local newString = convertString(stringToConvert, colors)
	script.Parent.Text = newString
end)

yep that should work, tell me if it doesnt and if there are any errors!

right before i type print:


after;

1 Like

You should have two different Instances: one is a TextBox and one is a TextLabel (the reason being the problem you ran into).

The TextBox should have invisible text (TextTransparency = 1) so that it doesn’t show what the user is typing (it only serves as type input detection)

The TextLabel should be overlapped with the TextBox and display what the user is typing.

TextBox:GetPropertyChangedSignal("Text"):Connect(function()
    TextLabel.Text = TextBox.Text -- The text label will be updated to show what the user typed
    -- Run the syntax highlight code here and update the TextLabel text, not the TextBox text
end)

Essentially, the TextLabel is the actual text editor that has all the colors and configurations, and the TextBox should be completely invisible, as it only serves to detect user input. (Copy the current TextBox you have, but it make it a TextLabel, then make the old TextBox invisible)

i know why that happens give me some time to fix it

function convertString(stringToConvert: string, colors)
	local newString = stringToConvert
	for keyword, newColor : Color3 in pairs(colors) do
		
		
		newString = string.gsub(newString, '%f[%a]'.. keyword.. '%f[^%a]', '<font color="rgb('.. tostring(math.floor(newColor.R * 255)).. ','.. tostring(math.floor(newColor.G * 255)).. ','.. tostring(math.floor(newColor.B * 255)).. ')">'.. keyword.. '</font>')	
	end
	
	return newString
end

local stringToConvert = '<font color="rgb(204,0,0)">local</font> variable = nil <font color="rgb(0,229,255)">print</font>("hello")'
local colors = {
	['local'] = Color3.fromRGB(204, 0, 0),
	['print'] = Color3.fromRGB(0, 229, 255)
}


local newString = convertString(stringToConvert, colors)

print(newString)

This code should work, this time it won’t infinitely replace itself.

still does it infinitely

function convertString(stringToConvert: string, colors)
	local newString = stringToConvert
	for keyword, newColor : Color3 in pairs(colors) do


		newString = string.gsub(newString, '%f[%a]'.. keyword.. '%f[^%a]', '<font color="rgb('.. tostring(math.floor(newColor.R * 255)).. ','.. tostring(math.floor(newColor.G * 255)).. ','.. tostring(math.floor(newColor.B * 255)).. ')">'.. keyword.. '</font>')	
	end

	return newString
end

local colors = {
	['local'] = Color3.fromRGB(204, 0, 0),
	['print'] = Color3.fromRGB(0, 229, 255)
}

script.Parent:GetPropertyChangedSignal("Text"):Connect(function()
	local stringToConvert = script.Parent.Text
	local newString = convertString(stringToConvert, colors)
	script.Parent.Text = newString
end)


try @HugeCoolboy2007 's idea and mix it with mine. My code works for changing the colors and his code would make it much easier to manage what text is actually inputted, and would also fix this issue.