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

How would I use the typewriter text effect on a table?

Asked by 5 years ago

So I am wanting to use the "typewriter effect" for a few messages. I know you would usually use the following:

local text = "Whatever text you want this to be"

for i = 1, #text do
    script.Parent.Parent.Dialog.TextLabel.Text = string.sub(text, 1, i)
    wait(0.04) --This is the speed of the text
end

However my text is in a table, and I am struggling to adjust the code to work using a table.

local Messages = {
     "Text",
     "Hello",
     "Testing",
 }



while true do
 for i,v in pairs(Messages) do
  wait(math.random(10,15))
   script.Parent.Text = string.sub(v, 1, i)
 end
end

2 answers

Log in to vote
0
Answered by
chomboghai 2044 Moderation Voter Community Moderator
5 years ago

It's the exact same thing you're doing for a single word:

for i = 1, #text do
    script.Parent.Parent.Dialog.TextLabel.Text = string.sub(text, 1, i)
    wait(0.04) --This is the speed of the text
end

except instead of using a single word, you just loop through the words in the table.

for _, message in pairs(Messages) do
    for i = 1, #message do
        script.Parent.Parent.Dialog.TextLabel.Text = string.sub(text, 1, i)
        wait(0.04)
    end
end
Ad
Log in to vote
0
Answered by
Mr_Unlucky 1085 Moderation Voter
5 years ago

I've tried getting a similar effect in my games, and I've successfully found a way.

Let's say you have a StringValue in the ReplicatedStorage. Every time it changes a TextLabel's text gives the typewriter text effect you want. This is very simple.

local StringValue = game:GetService("ReplicatedStorage").Message
local TextLabel = script.Parent

local function changeText()
    for i = 1,StringValue.Value:len() do
        TextLabel.Text = StringValue.Value:sub(1,i)
        wait(0.01)
    end
end

StringValue:GetPropertyChangedSignal("Value"):Connect(changeText)

In the code, we have two variables, one for the StringValue and the other for the TextLabel. We make a function that fires every time the StringValue's value is changed.

Ok, now for the function, it'll create a loop that repeats by the number of the length of the value. For example, if the value was "Hello", the loop would repeat itself five times. Now since the "i" part represents the number we're looping at currently we'll change the TextLabel's text to the cut up part of the characters one to the actual number we're on. This is kinda hard to explain and there's something that's most likely wrong, but here's a hypothetical scenario.

Let's say the Value is changed into "Hello World!"

It'll loop through the amount of characters in the actual sentence, so it's 12 times. The "i" value changes through the amount of times we've looped so far, so if we've looped five times the value of "i" is five. Okay, with this we'll cut apart the StringValue's value into the first few characters that "i" represents. So if the current value of "i" is five, the part we've just cut apart will be "Hello". We then use this to change the text to the cut apart message.

If we were to print the messages every time it is changed it'll be like this:

H
He
Hel
Hell
Hello
Hello 
Hello W
Hello Wo
Hello Wor
Hello Worl
Hello World
Hello World!

Hope this helped! Please point out any mistakes as explaining the code was difficult to explain.

Answer this question