Coloring Coding a String To Code

Hello Developers,

I am making a script executor for developers of a game and I want the script executor to be color coded, similar to the Roblox script executor. I am wondering if there are available community resources, or an easier way to do this, instead of manually checking the string and parsing each argument to change its color.

1 Like

bru that sounds complicated. good luck

2 Likes

I believe there aren’t any available resource out there to solve your needs, however you can try using the RichText property and the template <font color="rgb(0,0,0)"> text here </font> for the TextLabel?

1 Like

Hi @asherppan,

The following snippet should help you get started! Do keep in mind that with each new line, you would have to add a space at the beginning in order for it to work properly.

local colorStrings = {[Color3.fromRGB(255, 82, 82)] = {"local", "function", "end"}, [Color3.fromRGB(84, 161, 255)] = {"coroutine"}}

local function parseString(str) 
	local splitStr = tostring(str):lower():split(" ")
	local strs = {}
	for _, st in pairs(splitStr) do
		local found;
		for color, s in pairs(colorStrings) do
			if table.find(s, st:split(" ")[1]) then
				found = true
				table.insert(strs, {text = st, color = color})
			end
		end
		if found ~= true then
			table.insert(strs, {text = st, color = Color3.fromRGB(255, 255, 255)})
		end
	end
	return strs
end

local function convertToStr(t) 
	local str = ""
	for i, v in pairs(t) do
		local hex = string.format("#%02X%02X%02X", v.color.R * 0xFF, v.color.G * 0xFF, v.color.B * 0xFF)
		str = str .. `<font color="` .. hex .. `">` .. v.text .. `</font> `
	end
	return str
end

local code = parseString([[local test
 local function test() 
	print("Test")
 end]])

script.Parent.Text = convertToStr(code)

Something like this should help you get started, but it should not be used in your final project!
image

For every new line (designated as “\n”), you could make a section where it adds a " ".

1 Like

or use @boatbomber’s syntax highlighter

Where is that found? I can’t find the actual module on github.

This should be it, the rbxm file can be found in Releases

1 Like

When I’m using the module, it returns an error
“Attempt to index nil with Text ‘Text’”
Meaning the TextObject would return nil, but when directly indexing the TextObject, for instance print(TextObject) it would print the correct textobject but when used in the module, it returns nil.

I am using the module exactly as instructed on the github, and I know it’s not the original indexing because other properties are being edited.

Highlighter.highlight({props})

Is the module meant to be used on a serverscript?

What’s props here / what does props contain?

the text object and other optional parameters

i fixed it now, i just directly referenced the textobject from the module instead of from the script that calls it

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.