Custom Textbox Syntax Highlighting

I tried to “make” custom textbox syntax highlighting like the visual studio code one, the code is not resulting in any error but it’s still not working.
Any help is appreciated!

Code:


local keywordColors = {
	["if"] = Color3.fromRGB(255, 0, 0),
	["then"] = Color3.fromRGB(255, 0, 0),
	["end"] = Color3.fromRGB(255, 0, 0),
	["function"] = Color3.fromRGB(0, 255, 0),
}


local textBox = script.Parent.Codebox

local function updateSyntaxHighlighting()
	local text = textBox.Text

	for keyword, color in pairs(keywordColors) do
		local start, finish = 1, 1
		while finish do
			start, finish = string.find(text, "%f[%a_]" .. keyword .. "%f[%A_]", finish + 1)
			if start then
				local tag = string.format("<font color=\"#%02x%02x%02x\">", color.r * 255, color.g * 255, color.b * 255)
				text = string.sub(text, 1, start - 1) .. tag ..
					string.sub(text, start, finish) .. "</font>" .. string.sub(text, finish + 1)
			end
		end
	end

	textBox.RichText = true
	textBox.Text = text
end


textBox.FocusLost:Connect(updateSyntaxHighlighting)

Please keep in mind that i used a little bit of “Chatgpt” to fix this code but still no success…

Is the textbox not able to convert the color? What does it show inside the textbox? Please provide some images so we can see more clearly on what is going on.

Sorry for my late response but no it does not change anything in the textbox, it basically stays the same no new text added…

From what I can see, there are 2 things that probably will fix your problem.

  1. Try putting wait() inside the loop.
  2. From this line
local tag = string.format("<font color=\"#%02x%02x%02x\">", color.r * 255, color.g * 255, color.b * 255)

Since you are convert the decimal number to hexadecimal anyways for the code, you can just simply put the decimal value in there. Like this

local testColor = {255,255,255}

s = string.format("<font color=\"#%02x%02x%02x\">", testColor[1],testColor[2],testColor[3])
print(s) -- <font color="#ffffff">
  1. Try logging it and see what the text actually is. Try logging everything out and tracking how your code is operated.