Why does the text box behave like this?

Here is the code:

local text_box = script.Parent

local auto_color_lookup_tbl = {
	["script"] = Color3.fromRGB(255, 216, 119),
	["local"]  = Color3.fromRGB(191, 109, 239),
	["function"] = Color3.fromRGB(191, 109, 239),
	["game"]     = Color3.fromRGB(255, 216, 119),
	["Color3"] = Color3.fromRGB(255, 216, 119),

local type_lookup_tbl = {
	["boolean"] = Color3.fromRGB(32, 54, 255),
	["number"]  = Color3.new(0.454902, 0.921569, 0.403922)

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)

	local text = text_box.Text
	for keyword, color in pairs(auto_color_lookup_tbl) do
		if string.find(text, keyword) then
			local str, _ = string.gsub(text, keyword, [[<font color="]] .. rgb255richtext(color) .. [[">]] .. keyword .. [[</font>]])	
			text_box.Text = str

Here is the video of it happening:

Its because you have the TextBox connected to a Changed event.

Everytime the text in the box changes, the function will run, then the first time you type “local”, the function finds the local word in the text, and proceed to change the text “local” to <font color="rgb(191, 109, 239)">local</font> so the TextBox changed again… firing the Changed event again, now the text received is <font color="rgb(191, 109, 239)">local</font> then tries to find the word “local” which is there, so it perform the text change again to <font color="rgb(191, 109, 239)"><font color="rgb(191, 109, 239)">local</font></font>… and yeah… because the text in box changed again the event will fire again to the infinity… like a infinite loop constantly adding more and more text.

Well actually the engine will notice that, and will cancel the loop after a few iterations, with a Error like this in console:
Maximum event re-entrancy depth exceeded for Instance.TextChanged


You know what I’m trying to do here, how do I fix this?

Seems that you are trying to listen actively the text that player inputs, to give somekind of shortcuts. So I type local and the text becomes <font color="rgb(191, 109, 239)">local</font>

But, everytime player inputs a new character even a space, the function will run again finding the word “local” so it will get updated again over and over.

Under the approach you are using I dont think that can be fixed. By adding a variable to compare current text with the previous edited one, if those are the same then no apply changes… but that will work only the first time, and when player adds any new character the strings will be different and the text edit will run again.

A more complicated approach… could be creating a table/dictionary of the words that has been changed, so the function will check if parts of the string were already edited or not, to decide if perform editing or not…

Another approach could be, find the word “local” yes but if the word is surrounded by >local< these symbols, then do not perform the text editing. Mainly if the keywords found mets that requirement cancel the editing.