Can't Color Text?

Hey there, Im trying to color text from a script and it seems not to be working. It does print “found” but the text in the textbox does not change colors. Please help if you can.

local keywords = {
	{keyword = "local", color = Color3.new(0, 0, 1)},
	{keyword = "game", color = Color3.new(1, 0.666667, 0)},
	{keyword = "getservice", color = Color3.new(0, 0.666667, 1)}
}

script.Parent:GetPropertyChangedSignal("Text"):Connect(function()
	local term = script.Parent.Text
	if term ~= "" then
		local words = term:split(",")
		for _, selectedKeyword in ipairs(keywords) do
			local keyword = selectedKeyword.keyword
			local textColor = selectedKeyword.color

			local foundWord = table.find(words, keyword)
			if foundWord then
				print('found')
				foundWord = string.format(foundWord, '<font color="rgb(255,165,0)">%s</font>')
			end
		end
		term = tostring(words)
	end
end)

Are you changing the text color of a TextBox? You just need to change the TextColor3 property of the TextBox.

Ok, try this!

local keywords = {
	{keyword = "local", color = Color3.new(0, 0, 1)},
	{keyword = "game", color = Color3.new(1, 0.666667, 0)},
	{keyword = "getservice", color = Color3.new(0, 0.666667, 1)}
}

script.Parent:GetPropertyChangedSignal("Text"):Connect(function()
	local term = script.Parent -- Here is what I changed!
	if term.Text ~= "" then
		local words = term.Text:split(",")
		for _, selectedKeyword in ipairs(keywords) do
			local keyword = selectedKeyword.keyword
			local textColor = selectedKeyword.color

			local foundWord = table.find(words, keyword)
			if foundWord then
				print('found')
				foundWord = string.format(foundWord, '<font color="rgb(255,165,0)">%s</font>')
			end
		end
		term.Text = tostring(words)
	end
end)

So, if you notice a change you should see this.

Do you see? when you storing property as a variable it will store as a variable, when you change that variable. It won’t change in property!

let’s say, you have a boolvalue in Workspace and the value is set to false!
Here’s an example:

local boolean = game.Workspace.BoolValue.Value
game.Workspace.BoolValue.Value = false

boolean = true

print(boolean) -- This will print "true"!
print(game.Workspace.BoolValue.Value) -- Well, this will print "false"
1 Like

You are using string.format wrong. The first parameter should be your formatstring. So if you switch around the parameters like this it should work.
foundWord = string.format('<font color="rgb(255,165,0)">%s</font>', foundWord)

Is “Rich Text” enabled? If not then thats why.

nope, its on still dont know…

I tried it, it still does not work :confused:

I have gotten this error in the output “Maximum event re-entrancy depth exceeded for Instance.TextChanged (x4)”…

I think it does work, but im having another issue at the moment.

I just noticed that you are trying to assign the variable words to the text, words is a table which you cant convert to a string. I’m assuming you made a logic mistake in your code here, you also don’t do anything with the variable foundWord after using string.format on it so it never gets assigned to anything.

then how can I get this to work? I have tried many attempts with nothing working.