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

Why does it print the full message in once?

Asked by
Scout9 17
7 years ago
Edited 7 years ago

local String = "Hello World!" local Length = string.len(String) text = script.Parent.TextLabel for i=1,Length do if i%2 == 0 then --if i is even text.Text = ( string.sub(String,1,i) ) --Print normally else --else it is odd text.Text = ( string.sub(String,1,i) .. "_" ) end end

This auto prints the "Hello World!" straight from when executed, while it should do something like this:

H H_ HE HE_

And further on.. why is this?

2 answers

Log in to vote
1
Answered by
Pyrondon 2089 Game Jam Winner Moderation Voter Community Moderator
7 years ago
Edited 7 years ago

The reason this happens is because there's no delay between changes of the text. You can easily fix this by adding a wait:

local String = "Hello World!"
local text = script.Parent.TextLabel

for i = 1, #String do --// You can use '#' to get the length of a string.

    if i%2 == 0 then --if i is even

        text.Text = ( string.sub(String,1,i) ) --Print normally

    else --else it is odd

        text.Text = ( string.sub(String,1,i) .. "_" )
    end
    wait(.1) --// This will wait for one tenth of a second before changing it again.
end

However, this won't get the effect you're looking for. You can get what you're trying to do with a few changes:

local String = "Hello World!"
local text = script.Parent.TextLabel

for i = 1, #String do --// You can use '#' to get the length of a string.
    text.Text = String:sub(1, i)..(i < #String and '_' or ''); --// This is Lua's version of a ternary operator, read more about it below.
    wait(.1);
end;

This adds an underscore after the text, unless it's the last character in the string. You can read more about the ternary operator here and here.

Hope this helped.

EDIT: As noted in chat, the above example would give you something like: H_, He_, Hel_. If you wanted H, H_, He, He_, you'll want something like this:

local String = "Hello World!"
local text = script.Parent.TextLabel

for i = 1, #String do
    text.Text = String:sub(1, i);
    wait(.1);
    if i < #String then
        text.Text = String:sub(1, i)..'_';
        wait(.1);
    end;
end;

Hope this helped.

Ad
Log in to vote
0
Answered by 7 years ago

It's running the whole for loop in a single frame. You should add a wait(0.5) before the last end so that in each intermediary step you can see the Text.

However, you have another issue: the printout will read:

H_ He Hel_ Hell Hello_ etc

, adding both the next letter and the next underscore every other step, when (I think) you want it to read more like:

_ H H_ He He_

Which can be accomplished by simply having two instructions in each loop like so:

local String = "Hello World!"

local Length = string.len(String)

text = script.Parent.TextLabel

for i=1,Length do
    text.Text = ( string.sub(String,1,i-1) .. "_" ) -- Change i to i-1
    wait(0.5)
    text.Text = ( string.sub(String,1,i) ) -- Print normally
    wait(0.5)
end

Answer this question