Changing text easier

I am trying to make the text on a gui seem like someone is typing it. I could do this but its too long

chat = script.Parent


chat.Text = "H"
wait(0.1)
chat.Text = "He"
wait(0.1)
chat.Text = "Hel"
wait(0.1)
chat.Text = "Hell"
wait(0.1)
chat.Text = "Hello"
wait(0.1)

How could I do this easier? Because making whole scentences will take a long time and alot of lines.

You could try lopping through a string and for every character a letter is added to the text.

This should help you.

local text = "Hello"

for i = 0, #text, 1 do
    chat.Text = string.sub(text, 1, i)
    wait(0.1)
end

Ill try that, give me a second and ill reply if it works!

You can easily do this with some string magic.

local Text = "Hello"
for i = 1, #Text do
    chat.Text = string.sub(Text, 1, i)
    wait(.1)
end

What this does is loop through a for loop which adds by 1 until it reaches the amount of letters in the Text, which is Hello (5 letters). Using string.sub basically subtracts any useless letters in that string that you don’t want in there.

If I did for instance:

local text = "Hello"
local sound = game.ReplicatedStorage.Sounds

for i = 0, #text, 1 do
    chat.Text = string.sub(text, 1, i)
sound.KeySound:Play()
    wait(0.1)
end

Would work?

Yes, but I would recommend doing for i = 1, #text instead of for i = 0, #text, 1 do so you don’t have string.sub being string.sub(text, 1, 0) on startup.

When I added the :play() it made the sound one time but not 5 times.

I also did change the i = 0 to i = 1. Not much of a difference but thanks for pointing that out.

The reason it played 5 times is because you have the sound playing inside of the for loop. Take it outside like this.

local text = "Hello"
local sound = game.ReplicatedStorage.Sounds

sound.KeySound:Play()
for i = 1, #text do
    chat.Text = string.sub(text, 1, i)
    wait(0.1)
end

Thanks yall, its working now. A big thanks to instance0new and rsbplays for giving me the code and a thanks to zQ86 for fixing it!

1 Like

Make sure you mark a solution so people don’t click on this to help you even though there’s already been a solution.

2 Likes