Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How Can You Make A Non-Repetitive Print Script?

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.

3 answers

✓ Accepted answer
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)

4 comments

0

Thanks. 1+ for that one.

0

Interesting solution! I normally use a for loop with string.len but this is more elegant. I'm wondering if this is also a better method in terms of efficiency!

0

String.gmatch is MUCH faster than looping with string.len. I discovered this while using a function to return all characters in a string, using your for loop solution. I then utilised string.gmatch instead, and the speed increase was amazing when using large strings.

0

I profiled this and didn't see any more than a 10% speed up from using match over a traditional loop (although using string.len instead of the # operator was slower, but that's because of the table access in that expression, not the operation of finding length). I find the straightforward infringementing loop solution would be more understandable in most cases, so do you have a good example of cod

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

3 comments

2

Be careful, delay is an actual function in ROBLOX's libaries, so that variable name should be avoided for clarity.

1

Thanks for pointing that out, BlueTaslem!

0

Awesome, +1

("Hello"):gsub('.',function(x)script.Parent.Text=script.Parent.Text..x end)

gsub is fun.

This discussion is preserved as it was. New answers, comments, and votes are closed.