Greetings, Scripting Helpers community... I've been wondering, is there an efficient way to get a custom message to print letter by letter into a TextLabel box? I'd like any answers of script to be efficient, and non-repetitive. E.g,
T = script.Parent T.Text = "H" wait(0.1) T.Text = "He" wait(0.1) T.Text = "Hel" wait(0.1) T.Text = "Hell" wait(0.1) T.Text = "Hello" wait(0.1) T.Text = "Hello!"
Thank you. And remember, preferably NOT as seen above.
function addMessage(Object, Text, timeBetween) for v in string.gmatch(Text, ".") do --For every letter in Text Object.Text = Object.Text..v --Add it to Object's Text wait(timeBetween) end end addMessage(script.Parent, "Hello!", 0.1)
I know that Articulating has a good answer and that using string.gmatch
is a more efficient way of looping through each individual character, but there's one slight problem with it!
Namely, that you can't use a timeBetween
of less than 1/30 - which I found was quite annoying when writing a similar function myself, I wanted a way for much longer strings to still animate, but not take so long, which they would, even with the minimum delay. Think about it, a 60 character sentence would take 2 whole seconds, or even worse, this paragraph would take precisely 13 seconds!
One way of compensating for this is to add more than 1 character from the input string to the output Text
field per iteration. This method also uses coroutines so that you can easily schedule multiple simultaneous text animations, it also calculates how many characters to add per iteration, so this allows you to use delay
times of much less than 1/30.
function TypeLabel(label, text, characterDelay) coroutine.resume(coroutine.create(function() local step = characterDelay and math.ceil(1/(60*characterDelay)) or 1 local len = text:len() for i = 1, step * (math.floor(len/step) + 1)+1, step do d = i > len and len or i label.Text = text:sub(1,d) wait(characterDelay) end end)) end
("Hello"):gsub('.',function(x)script.Parent.Text=script.Parent.Text..x end)
gsub is fun.