Coding a Question

Remember how you wrote a sentence for your story, then swapped a word out for a placeholder? It's time to give players a chance to add something to your experience.

In the script, the placeholder you made will be a variable. In coding, variables are placeholders for information, in this case a word.

You'll start by asking players a question. Then, they'll type in an answer that gets stored in the variable.

Creating a Variable

Variables have names that tell programmers what they store. In this case, you'll create a variable called name1 for the placeholder.

  1. Click below the dashed lines and type local name1.


    1-- GLOBAL VARIABLES
    2local storyMaker = require(script:WaitForChild("StoryMaker"))
    3
    4-- Code controlling the game
    5local playing = true
    6
    7while playing do
    8 storyMaker:Reset()
    9
    10 -- Code story between the dashes
    11 -- =============================================
    12 local name1
    13
    14 -- =============================================
    15
    16 -- Add the story variable between the parenthesis below
    17 storyMaker:Write()
    18
    19 -- Play again?
    20 playing = storyMaker:PlayAgain()
    21end
    22

Setting a Variable

Now players need to have a chance to put something inside the placeholder. To change a variable, it needs to be set to something using the = symbol.

  1. After name1, make sure to add a space and then type =.


    1while playing do
    2 storyMaker:Reset()
    3
    4 -- Code story between the dashes
    5 -- =============================================
    6 local name1 =
    7
    8 -- =============================================
    9
    10 -- Add the story variable between the parenthesis below
    11 storyMaker:Write()
    12end
    13
  2. After the equal sign, type storyMaker:GetInput(). The code must be typed exactly as is, and capital letters must match.


    1while playing do
    2 storyMaker:Reset()
    3
    4 -- Code story between the dashes
    5 -- =============================================
    6 local name1 = storyMaker:GetInput()
    7
    8 -- =============================================
    9
    10 -- Add the story variable between the parenthesis below
    11 storyMaker:Write()
    12end
    13

Typing a Question

Variables can store different types of data including small numbers, true or false values, and strings. String type variables are special because they can store whole sentences. It's easy to spot string type variables because they're always in quotation marks "like this".

The question to ask players will be a string variable.

  1. In GetInput(), click between the parentheses. Inside type a question enclosed by quotation marks.


    1 -- Code story between the dashes
    2 -- =============================================
    3 local name1 = storyMaker:GetInput("What is your favorite name?")
    4
    5 -- =============================================
    6end
    7