Randomizing Text

Learn how to use tables and math.random() to constantly randomize text.

by GalaxyGourmet

Author Avatar

Setting It Up

You can't randomize text without, well, actual text. For this tutorial, we'll use a TextLabel in a ScreenGui. Follow the steps listed to correctly set it up.

  1. Insert a ScreenGui in StarterGui. Name it whatever you'd like.
    
  2. Insert a TextLabel in that same ScreenGui. Name *that* whatever you'd like.
    
  3. Insert a LocalScript in that same TextLabel.
    

If you followed those steps correctly, you should see something like this in your explorer:

img|80x45

If you don't, you probably didn't follow the steps right. If you do, great! Let's move on.

Getting Into the Script

Now that we set up the scene, it's time we dive into that LocalScript. Open the LocalScript, and delete the print("Hello world!") line. To start off, let's add a simple variable.

local duration = 5

That variable will determine how long the text lasts before changing. We are changing this text too, y'know. Feel free to change it if you'd like. Nothing too fancy, but hey, we're about to get into the table.

Making the Table

Now that we got that variable ship shape, it's time to make a table. The table that we will make will contain the text that will be randomly selected. Insert the following below the variable we just made:

local text = {}

Congratulations! You just made an empty table! Yippee! The {} is basically a table. Remember that.

Ok, so we've just made an empty table. Now we have to put stuff in it. By stuff, I mean text. To do that, you'll have to make a string.

"This is a string."

Anything inside " is called a string. It's what the text is going to be. Ok, enough yapping, let's slap it in our little table.

local text = {
"Why hello there! I am text."
}

Alright. We've finally put something in our table. Let's go insert more stuff.

Putting More in the Table

Ok, so how do you make more stuff in a table? Well, it's fairly simple really. Just add a , like so:

local text = {
"Why hello there! I am text.",
"I am also text."
}

See how there is a , after the first string? That is how you seperate values from one another in a table. Let's add three more strings to the table.

local text = {
"Why hello there! I am text.",
"I am also text.",
"Same here.",
"I am not text.",
"Just kidding! I am."
}

Great. We have our text. Let's hurry up and randomize it.

Randomizing the Text

Here is where math.random() comes into play. Insert the following below the table:

local number = math.random(1, 5)

That would return a number between 1 and 5, 5 being the number of strings in the table. But, what if you have a long table, and don't want to count so much? There's a solution for that too, believe it or not.

local number = math.random(1, #text)

When you use #TableName, it would return the number of values listed in a table, and since we have 5 values in our table, #text would return 5.

Ok, now that we know how to do that stuff, let's make a loop and use it to randomize text.

local number = math.random(1, #text)

while true do
    script.Parent.Text = text[number]
    wait(duration)
end

Now let's break it down.

Breaking Down the Script

Of course, while true do is an infinite loop, but we want to look at the text[number] part, right?

TableName[number] will return the value listed in the specified order. For example, if we were to write the following:

text[1]

It would return whatever is the first value listed in the table, which is "Why hello there! I am text." and since we randomized that number, it would pick a random value, therefore, it would randomize the text that would appear, because we set the text to be what was chosen. So we did it right? Well, why not run the game and find out?

After testing it through, it probably did select random text, and made it appear, but however, it did not keep changing it. That's a problem. Let's fix that.

while true do
    local number = math.random(1, #text)
    script.Parent.Text = text[number]
    wait(duration)
end

Now try it. Is it finally looping? It probably is, but how?

Well, the math.random() variable was previously created before the loop, but if you've noticed, it's been moved inside the loop. What difference does that make? Let me explain.

That random number is something called a local variable. Local variables exist only in the block they're created in. The while true do is a block itself. The random number is created in the loop, but because it's a local variable, it gets deleted as the block ends. But because it's a loop, the block fires again, and the variable gets created again, and it selects another random number. Neat, huh? That's basically it! We've finally are able to randomize text infinitly! Hooray!

The Final Script

local duration = 5
local text = {
"Why hello there! I am text.",
"I am also text.",
"Same here.",
"I am not text.",
"Just kidding! I am."
}

while true do
    local number = math.random(1, #text)
    script.Parent.Text = text[number]
    wait(duration)
end

View in-game to comment, award, and more!