How can i make this in a shorter way?
local TextLabel = script.Parent.TextLabel TextLabel.Text = 'H' wait(0.1) TextLabel.Text = 'Hi' wait(0.1) TextLabel.Text = 'Hi T' wait(0.1) TextLabel.Text = 'Hi The' wait(0.1) TextLabel.Text = 'Hi Ther' wait(0.1) TextLabel.Text = 'Hi There' wait(0.1) TextLabel.Text = 'Hi There!' wait(0.1)
string.sub
, or alternatively strName:sub
, is the substring function. Basically, it gives you only the specified section of a string. The first number is the character you want to start at, and the second is the one you want to end at.
local str = "Hi there!" print( str:sub(1, 5) ) --> Hi th (spaces count as a character)
Or alternatively,
print( string.sub("Hi there!", 1, 5) ) --> Hi th
Although I prefer it in method form.
So, like I said before, the second number is where you want the section of the string to end. In the example above it ended at the fifth character. So to make a sort of typing simulation we have to increase that number once each time, e.g.
local str = "Hi there!" print( str:sub(1, 1) ) --> H print( str:sub(1, 2) ) --> Hi print( str:sub(1, 3) ) --> Hi (with a space) print( str:sub(1, 4) ) --> Hi t
etc.
But wait! What else do we know that can increase a number every time? A for
loop.
local str = "Hi there!" for i = 1, #str do --#str is the length of the string (here it's 9) print( str:sub(1, i) ) end