Any way to shorten this?

local Test = game.StarterGui.Gui.TextBox
Test.Text = "H"
wait(0.1)
Test.Text = "He"
wait(0.1)
Test.Text = "Hel"
wait(0.1)
Test.Text = "Hello"

Im trying to make this thing where the text gradually appears. The only way i know how to do it is listed above. Is there any method to shorten it?

2 Likes

Sure, so let me introduce you to the string global! The one we’ll be focusing on right now is string.sub(). string.sub() can return the substring of s that starts at i and continues until j. So, taking this knowledge and for i loops, we can simply do this.

local function TextEffect(Text)
    for i = 1,#Text, 1 do -- // This starts at 1 and ends at the amount of characters in Text.
        local subbedString = string.sub(Text,1,i) -- // Starts at the first character and gradually increases i, 
        --hence showing more of the string
        Test.Text = subbedString
    end
end
1 Like

I suggest adding a wait() in there, as I think @ChickHenEn wants to make a typewriter.

Oh yeah my bad, totally slipped my mind, thanks.

1 Like

If you’re creating a typewriter effect, you could try changing the MaxVisibleGraphemes property of the textlabel instead. Test.Text = “Hello” with MaxVisibleGraphemes set to 1 will read “H”, and you can tween/loop increment that value until you reach the size of the string.