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

How would it be possible to print a string letter by letter?

Asked by 7 years ago

I've tried doing this but with no luck;

string = "Hello"

while wait(0.5) do
print(string.sub(string,1,1))
end

I'm basically trying to make it so that it prints the string letter by letter every 0.5 seconds until it reaches the last letter. Example;

H He Hel Hell Hello

Thanks.

1 answer

Log in to vote
1
Answered by
nilVector 812 Moderation Voter
7 years ago
Edited 7 years ago

Firstly, you cannot use the word "string" as a variable, because it is a Lua keyword.

Aside from that, what you need to do is increment through all the letters of your string using a for loop. Here's how you would do it:

local word = "Hello" -- Your word
local length = string.len(word) -- This is the length of the word

for i = 1, length do -- Loops 5 times since "Hello" is 5 characters long
    -- Prints the word from the first letter to the character position of i
    print(string.sub(word, 1, i))
    wait(0.5)
end

Here's a link to the ROBLOX Wiki on for loops in case you're confused about what just I did. I highly recommend you read it if you don't understand how a for loop works. They are very important and useful for programmers. :)

Hope this helps! if it does, don't forget to click "Accept Answer" and +1 my Rep!

Ad

Answer this question