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.
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?
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!
For every new line (designated as “\n”), you could make a section where it adds a " ".
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.