Coloring words in string via script with typing effect

local function typeText(label , text, speed )
	for i = 1, #text do
		label.Text = string.sub(text, 1, i)
		task.wait(speed)
	end
end

I know that you can color text with html tags. But with typing function that’s not gonna really work since it will also type tag, which is not good.
Is there any other ways I can achieve coloring some specific words and keep typing effect?

1 Like

There is, I wrote this function that does that for you.

local textLabel = script.Parent.TextLabel
local textToType = "<i>Some</i> string"

local function typeText(label: TextLabel, text: string)
	label.MaxVisibleGraphemes = 0
	label.Text = text

	for i = 1, string.len(text) do
		label.MaxVisibleGraphemes = i
		
		task.wait(.1)
	end
end

typeText(textLabel, textToType)
2 Likes

Yes, by using rich text, and Graphemes Typewriter Effect: New property MaxVisibleGraphemes (Live) - #20 by StonksMcHat

1 Like

Give the solution to @Nitefal, but I just want to add some extra considerations for what you’re looping with (my basis for these are from this post):


Let’s have the string <i>some</i> some which would show up as “some some”

string.len() would also rich-text tags (like <i> and </i> as seperate characters, which is why it would return 16 characters. The post provides an example of a function that can be used to filter out tags:

local function removeTags(str)
	-- replace line break tags (otherwise grapheme loop will miss those linebreak characters)
	str = str:gsub("<br%s*/>", "\n")
	return (str:gsub("<[^<>]->", ""))
end

warn(string.len("<i>some</i> some")) -- 16,, includes tags :(
warn(string.len(removeTags("<i>some</i> some"))) -- 9,, yay!!

Now for emojis, let’s have the string “hi :fire:

string.len() considers emojis as 4 characters (because emojis are 4 bytes). The solution is to use utf8.len() instead which correctly considers a single emoji to be a single character.

warn(string.len("hi 🔥")) -- 7,, because an emoji equals 4...
warn(utf8.len("hi 🔥")) -- 4,, yay!!
1 Like

Yoo thanks :heart:

30charlimit
aaaaaaaaaa

Appreciate it for explaining , tysm :heart:

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