I need help with string detection

Hi, i’m trying to make a script GUI, and the thing i need help at the moment is to detect the text, for example:
If the player writes:
local hamburger = workspace.Hamburger
If the player writes that i want to detect the text and make the “local” text red and the “workspace” blue.
My friend tried making this with a TextBox and RichText but it didn’t work, here’s the code:

local function detect()
    for _ in string.gmatch(script.Parent.Text, "local") do
        script.Parent.Text = "<font color=rgb(255,0,0)>local</font>"
    end
end

If anyone needs mroe information about the topic or hints / tips on how i can do this, any help would be appreciatted. Thanks :slightly_smiling_face:
Also it doesn’t need to use richtext i just need it to change the color of the specific string.

Most people say there isn’t a way and I’m not sure if there is or isn’t but here.
I’ve briefly looked into it and this is the most relevant page I’ve found:

There is a specific users post that I haven’t tested yet but its worth a shot.

Edit:
I’ve found the API for this so I do believe this would be your solution:

1 Like

I’m pretty sure you can find string.find useful here.

2 Likes

Your friend is on the right track but he needs to use this code instead. The code above just loop sets the text to “< font color=rgb(255,0,0)>local” and doesn’t replace the text. I would recommend having a second value to store the unedited string then use the text field to store the edited version. Here is the code you would need:

local function detect()
    script.Parent.Text = string.gsub(script.Parent.Text, "local", "<font color=rgb(255,0,0)>local</font>")
end

Note: I am unsure if gsub is supported in luau but I believe it is.

gsub is supported in luau because luau is an extension of 5.1 and gsub has existed before even 5.1