Ad
Log in to vote
0

Trying to format text in string to change color of font?

Asked by
Troxure 87
15 days ago

Hi there, I am trying to change the color of specific terms in a string. For example, I would want certain words a different color than the rest, basically syntax. However I am running into issues and the text is not changing colors. The TextBox has RichText enabled too.

Here is the script:

local Terms = {
    "if",
    "then",
    "do",
    "while",
    "end",
    "break",
    "and"
}

local Position

while true do wait()
    Position = 1
    local TermTable = {}
    for i, Term in next, Terms do
        table.insert(TermTable, Term)
    end
    for i, Term in next, TermTable do
        if(i == Position and script.Parent.Text:find(Term)) then
            local Str = script.Parent.Text
            local StartPosition = Str:find(Term)
            local EndPosition = StartPosition + (#Term - 1)
            local TargetString = Str:sub(StartPosition, EndPosition)

            local function FormatString(S, C)
                local R,G,B = C.R*255,C.G*255,C.B*255
                return string.format("<font color=\"\rgb(%d, %d, %d)\"\>%s</font>",R, G, B, S)
            end

            local FormattedString = FormatString(TargetString, Color3.fromRGB(255, 85, 0))
            repeat wait() until FormattedString
            string.gsub(script.Parent.Text, TargetString, FormattedString)
            Position = Position + 1
        end
    end
end

Answer this question