Chaning specific words in textbox doesnt work

HIii i tried to create a textbox which makes so if a player writes some specific words itll be highlighted in a specific colour, like how roblox studio does when it comes to scripting but i cant get it working.

local myTextBox = script.Parent

local function setRichText(textBox, text)
	local richText = ""

	for _, word in ipairs(string.split(text, " ")) do
		local lowerWord = word:lower()
		local color = "White"  

		if lowerWord == "Apple" then
			color = "Red"
		elseif lowerWord == "Sea" then
			color = "Blue"
		end

		richText = richText .. "<font color=\"" .. color .. "\">" .. word .. "</font> "
	end

	textBox.RichText = richText
end

while true do
	setRichText(myTextBox, myTextBox.Text)
	task.wait()
end

you were trying to set the RichText property of the text box, but the RichText property is not available in the standard text box component. Instead, you should use the Text property to display the formatted text.

here is the changed code

local function setRichText(textBox, text)
local richText = “”
for _, word in ipairs(string.split(text, " “)) do
local lowerWord = word:lower()
local color = “White”
if lowerWord == “apple” then
color = “Red”
elseif lowerWord == “sea” then
color = “Blue”
end
richText = richText … “” … word … " ”
end
textBox.Text = richText
end
while true do
setRichText(myTextBox, myTextBox.Text)
task.wait()
end

this code u provided only made so i couldnt type any space in the textbox and the words never got changed to the colours

local myTextBox = script.Parent

local function setRichText(textBox, text)
	local richText = ""
	for _, word in ipairs(string.split(text, " ")) do
		local lowerWord = word:lower()
		local color = "White"
		if lowerWord == "local" then
			color = "Red"
		elseif lowerWord == "Sea" then
			color = "Blue"
		end
		richText = richText..""..word..""
end
textBox.Text = richText
end
while true do
	setRichText(myTextBox, myTextBox.Text)
	task.wait(0.2)
end

Is rich text enabled? Double check it!

ye im sure its enabled (if its this box u mean)
Screenshot 2024-02-21 205651

For detecting the word in the TextBox you can use string:find

local Thestring = "I want the orange candy!"
Thestring = Thestring:lower()

if Thestring:find("orange") then
   print("There's orange!")
end

For changing the color of a word you can use RichText , a property of TextLabel, TextButton, or TextBox and edit the color from there. (See the article for more information.)

1 Like

a better and easy to build on method would be using string.gsub, string.format and some tables

local DefinedWords = {
	Apple = Color3.fromRGB(255, 0, 0),
	Sea = Color3.fromRGB(0, 255, 0)
}

local RichTextFormat = "<font color=\"rgb(%d,%d,%d)\">%s</font>"
local function setRichText(textBox, text)
	for Word, Color in DefinedWords do
		print(string.format(RichTextFormat, Color.R, Color.G, Color.B, Word))
		text = string.gsub(text, Word, string.format(RichTextFormat, Color.R * 255, Color.G * 255, Color.B * 255, Word))
	end

	textBox.Text = text
end

also use the Text property, the RichText property is just a boolean : P

1 Like

Fixed the code i had to test it

local DefinedWords = {
	Apple = Color3.fromRGB(255, 0, 0),
	Sea = Color3.fromRGB(0, 255, 0)
}

local RichTextFormat = "<font color=\"rgb(%d,%d,%d)\">%s</font>"
local function setRichText(textBox, text)
	for Word, Color in DefinedWords do
		text = string.gsub(text, Word, string.format(RichTextFormat, Color.R * 255, Color.G * 255, Color.B * 255, Word))
	end

	textBox.Text = text
end

It kinda worked ig but like just a quick question, when i edit the textbox then i see <font but when i stop edit the textbox the word apple does become red. Is there any solution to that or fo i have to stick with somehow display the text in a textlabel whilst letting the player edit the textbox?

Yea if you are going to edit the text in game you should use a TextLabel which covers the textbox and do the rich text formatting on the label, otherwise you will be editing the format itself if you use textbox only

1 Like

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