[Help] How would i color a certain word in a textlabel

Hello, so. Let me get quickly to it.

I’m trying to make my own programming langue in Roblox Studio. The problem is, I’m trying to create an IDE text color thing. Like for example, as you type “local” or something it colors only that word a certain color for that certain world only.

I need an efficient and non-buggy way to do that. I’m not sure how I’d achieve that or write that.

This is sadly not possible, you could maybe try to programm a plugin but I am almost definite that this is not possible.

Oh wait I got it now… so basically I think you need to write an extra label to it and add an extra decal but I don’t really know how to create a custom programming language inside a game.

It is 100% possible with the help of rich text. The only issue is that rich text doesn’t have the best track record, so I’d be careful using it.

What do you mean by “doesn’t have the best track record”, what’s wrong with using it?

I don’t know. I have just seen many people complain about bugs with it.

Yeaaaaa sorry my bad, didn’t think about that.

Ye, I’m trying to do it with that. Just wondering what to do with the string events to make it functional

You could use enums to define different types of tokens, and then use a switch statement or if/else statements to color them differently.

local TokenType = {
    Keyword = 1,
    Identifier = 2,
    String = 3,
    Number = 4,
    Punctuation = 5,
    Operator = 6,
    Comment = 7,
}

local getTokenType = function(token)
    if isKeyword(token) then
        return TokenType.Keyword
    elseif isIdentifier(token) then
        return TokenType.Identifier
    elseif isString(token) then
        return TokenType.String
    elseif isNumber(token) then
        return TokenType.Number
    elseif isPunctuation(token) then
        return TokenType.Punctuation
    elseif isOperator(token) then
        return TokenType.Operator
    elseif isComment(token) then
        return TokenType.Comment
    end
end

local colorToken = function(tokenType, token)
    if tokenType == TokenType.Keyword then
        print("Keyword: " .. token)
    elseif tokenType == TokenType.Identifier then
        print("Identifier: " .. token)
    elseif tokenType == TokenType.String then
        print("String: " .. token)
    elseif tokenType == TokenType.Number then
        print("Number: " .. token)
    elseif tokenType == TokenType.Punctuation then
        print("Punctuation: " .. token)
    elseif tokenType == TokenType.Operator then
        print("Operator: " .. token)
    elseif tokenType == TokenType.Comment then
        print("Comment: " .. token)
    end
end

local lex = function(file)
    for line in file:lines() do
        for token in line:gmatch("([^%s]+)") do
            local tokenType = getTokenType(token)
            colorToken(tokenType, token)
        end
    end
end

lex(io.open("input.lua", "r"))
Output
Keyword: local
Keyword: function
Identifier: getTokenType
Punctuation: (
Identifier: token
Punctuation: )
Keyword: if
Identifier: isKeyword
Punctuation: (
Identifier: token
Punctuation: )
Keyword: then
Keyword: return
Identifier: TokenType
Punctuation: .
Identifier: Keyword
Punctuation: end
Keyword: elseif
Identifier: isIdentifier
Punctuation: (
Identifier: token
Punctuation: )
Keyword: then
Keyword: return
Identifier: TokenType
Punctuation: .
Identifier: Identifier
Punctuation: end
Keyword: elseif
Identifier: isString
Punctuation: (
Identifier: token
Punctuation: )
Keyword: then
Keyword: return
Identifier: TokenType
Punctuation: .
Identifier: String
Punctuation: end
Keyword: elseif
Identifier: isNumber
Punctuation: (
Identifier: token
Punctuation: )
Keyword: then
Keyword: return
Identifier: TokenType
Punctuation: .
Identifier: Number
Punctuation: end
Keyword: elseif
Identifier: isPunctuation
Punctuation: (
Identifier: token
Punctuation: )
Keyword: then
Keyword: return
Identifier: TokenType
Punctuation: .
Identifier: Punctuation
Punctuation: end
Keyword: elseif
Identifier: isOperator
Punctuation: (
Identifier: token
Punctuation: )
Keyword: then
Keyword: return
Identifier: TokenType
Punctuation: .
Identifier: Operator
Punctuation: end
Keyword: elseif
Identifier: isComment
Punctuation: (
Identifier: token
Punctuation: )
Keyword: then
Keyword: return
Identifier: TokenType
Punctuation: .
Identifier: Comment
Punctuation: end
Keyword: end
Keyword: local
Keyword: function
Identifier: colorToken
Punctuation: (
Identifier: tokenType
Punctuation: ,
Identifier: token
Punctuation: )
Keyword: if
Identifier: tokenType
Operator: ==
Identifier: TokenType
Punctuation: .
Identifier: Keyword
Keyword: then
Identifier: print
Punctuation: (
String: Keyword:
Punctuation: ..
Identifier: token
Punctuation: )
Keyword: elseif
Identifier: tokenType
Operator: ==
Identifier: TokenType
Punctuation: .
Identifier: Identifier
Keyword: then
Identifier: print
Punctuation: (
String: Identifier:
Punctuation: ..
Identifier: token
Punctuation: )
Keyword: elseif
Identifier: tokenType
Operator: ==
Identifier: TokenType
Punctuation: .
Identifier: String
Keyword: then
Identifier: print
Punctuation: (
String: String:
Punctuation: ..
Identifier: token
Punctuation: )
Keyword: elseif
Identifier: tokenType
Operator: ==
Identifier: TokenType
Punctuation: .
Identifier: Number
Keyword: then
Identifier: print
Punctuation: (
String: Number:
Punctuation: ..
Identifier: token
Punctuation: )
Keyword: elseif
Identifier: tokenType
Operator: ==
Identifier: TokenType
Punctuation: .
Identifier: Punctuation
Keyword: then
Identifier: print
Punctuation: (
String: Punctuation:
Punctuation: ..
Identifier: token
Punctuation: )
Keyword: elseif
Identifier: tokenType
Operator: ==
Identifier: TokenType
Punctuation: .
Identifier: Operator
Keyword: then
Identifier: print
Punctuation: (
String: Operator:
Punctuation: ..
Identifier: token
Punctuation: )
Keyword: elseif
Identifier: tokenType
Operator: ==
Identifier: TokenType
Punctuation: .
Identifier: Comment
Keyword: then
Identifier: print
Punctuation: (
String: Comment:
Punctuation: ..
Identifier: token
Punctuation: )
Keyword: end
Keyword: end
Keyword: local
Keyword: function
Identifier: lex
Punctuation: (
Identifier: file
Punctuation: )
Keyword: for
Identifier: line
Keyword: in
Identifier: file
Punctuation: :
Identifier: lines
Punctuation: (
Punctuation: )
Keyword: do
Keyword: for
Identifier: token
Keyword: in
Identifier: line
Punctuation: :
Identifier: gmatch
Punctuation: (
String: [^%s]+
Punctuation: )
Keyword: do
Identifier: local
Identifier: tokenType
Operator: =
Identifier: getTokenType
Punctuation: (
Identifier: token
Punctuation: )
Identifier: colorToken
Punctuation: (
Identifier: tokenType
Punctuation: ,
Identifier: token
Punctuation: )
Keyword: end
Keyword: end
Keyword: end

I love coding.

1 Like

That’s some pretty clear stuff right there. I’ll give it a try.

This is what you are looking for: GitHub - boatbomber/Highlighter: RichText highlighting Lua code with a pure Lua lexer

The method above is super messy and not ideal for readability.

Alrighty, tell me if it worked.