How do you make an Textbox look like code from the script editor?

How do you make an textbox change colors like the Script Editor?

Example:
Screenshot 2021-03-10 171059

1 Like

I’m not too sure as I’ve never used it but RichText and the code font will help you here

Make a table with keywords as table’s value indexes and colors as the index’s value

table = { ["keyword"] = color }       

then connect a .Changed event to the textbox and run a loop that would check if the word inside the sentence is the word from the aforementioned table. if it is (make sure the textbox has richtext on) - change the color of the word.

1 Like

I ain’t that good in lua, could you give me an example?

for word, color in pairs(words) do
   local format = string.format("<font color=\"rgb(%d, %d, %d)\">%s</font>", color.R, color.G, color.B, word)
   -- automatically formats the string for the textbox. Example Result: <font color="rgb(255, 0, 0)">keyword</font>
   -- do stuff
end

You could use the RBX Lexer for detecting keywords and tokens as syntax highlighting isn’t exactly easy.

I didn’t understand that :frowning: Can you show me an example on how to make comments grey maybe?

I’m not good with syntax highlighting so i’d only be able to give you this:

local font_color = "<font color=\"rgb(%d, %d, %d)\">%s</font>" -- default for the syntax highlighting
local comment_string = "%-%-.+" -- comment string format

local function getComment(_string: string, pattern: string) : string | nil
	return _string:match(pattern) -- this returns whether not not it was able to find a string match with the string pattern
end

local function formatComment(_string: string) : string
	local s = getComment(_string, comment_string)

	if s then -- check if the string was a match
		return font_color:format(65, 65, 65, s) -- this formats the string into xml and returns it
	end

	return "" -- this returns a default blank value
end

print(formatComment("-- this is a comment")) -- this prints the formatted xml comment

For you to do your own syntax highlighting, you’d have to learn string patterns and rich text