Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can i do this better?

Asked by 9 years ago

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)


1
I don't have time to make a script, but here is what I'd do: Make a variable and set it to your string that you want. Then make a for loop that runs the length of how many digits are in your string. Then make a temporary variable that is set to the string variable, but cut down to the length of your loop iterator. Don't forget a wait() so it has some time. GShocked 150 — 9y

1 answer

Log in to vote
2
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

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
Ad

Answer this question