Better method to accomplish this simple text effect (typewriter) via for loop?

The bellow code works but It feels rudimentary:

local BundlesWord={"B","Bu","Bun","Bund","Bundl","Bundle","Bundles"}

for i=1, #BundlesWord, 1 do
Bundles.Text=BundlesWord[i]
wait(.1)
end

I’ve been curious about a better method of going about this, surely I don’t have to do the local BundlesWord step by step like that right?

1 Like
local BundlesWord = "Bundles"

for i = 1, #BundlesWord do
    Bundles.Text = string.sub(BundlesWord, 1, i)
    wait(.1)
end

Length operator works for strings as well

4 Likes

Another solution for this that works well with TextScaled (meaning the whole text wont move around randomly as the effect adds on text) is the roblox provided Typewriter effect. It uses MaxVisibleGraphemes instead of setting the text itself, which also will help a lot if you want to translate your game’s text later.
Found here:

2 Likes

Also, a good way to use, EmbatTheHybrid’s solution, is making it into a module script! Like so:

Module Script:
Path: game.ReplicatedStorage.Module’sNameHere

local TextMethods = {}

function TextMethods.TypeWrite(String, Label, charDelay)
    for chars = 1, #String do
        Label.Text = string.sub(String, 1, chars)
        wait(charDelay)
    end
end

Local Script:
Path: Inside, any kind of UI element, that contains Text

local TextMethods = require(game.ReplicatedStorage:FindFirstChild("Module'sNameHere"))

local Label = script.Parent --Change to the Label path!
local Delay = 0.08 --Change to the prefered delay!
local String = "Bundles" --Change to the actual string, you want to TypeWrite!

TextMethods.TypeWrite(String, Label, Delay)

I hope, this helped, specifically, for not having to write the code every time you want to create this effect, and just having it in a module script! Keep Embat’s reply as the solution, as he was the person who actually solved this! If you find any errors, let me know, because I am actually doing this from mobile, and it’s really hard to check for errors!

1 Like