Can someone break this code down to me?

local message = "UNSEEN INTERACTIVE"
local label = --where ever the gui is located

for i = 1, #message do
    label.Text = string.sub(message, 1, i)
    task.wait(.05)
end

I saw this script in a post and I wanna know how the for loops and string.sub() works. I don’t do these stuff a lot so I’m a bit clueless XD

Simply put, this is writing the text “UNSEEN INTERACTIVE” out letter by letter.

The first two lines are declaring the variables for the TextLabel and text to typewriter.

for i = 1, #message do is looping for the length of the message, with i representing the current index.

label.Text = string.sub(message, 1, i) is setting the the text to a copy of the message split from the beginning (1) to the index (i). Since i represents the current index, a new letter will be added each time the loop loops.

task.wait(.05) basically just delays execution so all of the text doesn’t appear in one go.

3 Likes

thanks! appreciate you taking your time to help me :slight_smile:

2 Likes

alright, so lets break down the loop first.
for i = 1, #message
this means this loop will run for #message times.
#message is the length of the local message string given.

for example, in the script you showed, message is “UNSEEN INTERACTIVE” which has a total of 18 characters. Thus, your loop will run 18 times.
________

now, string.sub is used to get a portion of text from a string.
For example its used to get the first 5 letters of a word, or the last 3 letters etc.

the first thing you put in the brackets of string.sub() is the string you want to shorten/get a part of.
In this case, we will put the message in the brackets as such : string.sub(message)

the second thing you put in the brackets of string.sub() is the number you want to start from,
Example, if you put 3, the result of string.sub will start from the third letter/character of the string.

Example Code :

local your_string = "HEY This is a TEST!"
local result = string.sub(your_string,2,#your_string)
print(result)

-- this will print 'EY This is a TEST!` (because we put 2 in the middle, it started from the second letter)

Now the third, thing you put in the brackets of string.sub() is the number till where the string will go.
Example you put the number 5 in there. So the String will start from the number you put in the second section to the number you put in the third section.

Example - string.sub(your_string,1,5)
this means the result will start from the first letter and end at the 5th letter. it will ignore all letters ahead the 5th letter.

________

sorry if im bad at explaining, i tried my BEST To detail it out.

1 Like

amazing I understood everything you are very detailed and good at explaining tysm!

1 Like

It’s worth noting that this isn’t a good method anymore ever since the MaxVisibleGraphemes property came out. MaxVisibleGraphemes essentially says how many characters appear, so:

MaxVisibleGraphemes = 3 would give you “UNS”
MaxVisibleGraphemes = 10 would give you “UNSEEN INT”

and

MaxVisibleGraphemes = -1 would give you the whole string, because -1 is no limit

hey! thanks for the new info I asked to explain it but this is very useful! Could you provide an example of how I would integrate this? I’ve never heard of it before.

It’s pretty similar to the original method, just instead of setting the text each time, you change MaxVisibleGraphemes by 1.

Example:

local TextLabel = script.Parent
TextLabel.MaxVisibleGraphemes = 0

for i in utf8.graphemes(TextLabel.Text) do
	TextLabel.MaxVisibleGraphemes = i		
	task.wait(0.05)	
end

This would gradually add characters back in (without manually changing the text). I’ll explain it more in the dropdown below, if that’s helpful

More Details
local TextLabel = script.Parent
TextLabel.MaxVisibleGraphemes = 0

This part is straightforward, just define the textlabel and set it’s MaxVisibleGraphemes to 0 (Essentially hiding the text, since it says it can show 0 characters)

for i in utf8.graphemes(TextLabel.Text) do

This is pretty similar to an for i,v in pairs(Table) do loop. The only differences here is that:

  1. No v variable (utf8.graphemes does give us a v, but in this case with this simple text, it’s the exact same as i).
  2. i variable shows the current length that it’s at (utf8.graphemes just basically goes through each individual character)
	TextLabel.MaxVisibleGraphemes = i		
	task.wait(0.05)	
end

As I said earlier, i is where it’s currently at in the iterator, so it sets the max visible characters to that.

You might be wondering why we wouldn’t just use #TextLabel.Text, but that’s because the raw length is different than the visible “graphemes” (basically characters). The reason this matters is because things like emojis technically aren’t one piece of length, they’re three (this is why utf8.graphemes has 2 things it returns, one for where a grapheme started and one for where it ended. For emojis, it could be like 1,3 since it started at position 1 and ended at position 3).

To make this simple it pretty much just changes the text for a text label to the seen text in quote marks. It will only do t his one time.