Lua (Im a beginner) I wanted to make a true or false game but It keeps saying "lua: main.lua:27: 'end' expected (to close 'if' at line 5) near '<eof>'"

Question: What am I doing wrong?

Current behavior: It keeps saying “lua: main.lua:27: ‘end’ expected (to close ‘if’ at line 5) near ‘’”

Desired behavior I wanted to make a true or false game

Repl link: https://replit.com/@PeytonRhiley/FrayedSnivelingSystemsoftware#main.lua

code snippet
(it wont work)

This is why you should indent your code properly so it is easy to find these errors

function questionChoice()
  if choice == "yes" then
     print("True or False")
     print("Do camels have three sets of eyelids?")
  if choice == "true" then
     print("You are... Corect!") 
  if choice == "false" then
    print("You are... Wrong.") 
  elseif choice == "no" then
    print("Okay goodbye!")
  end
end
elseif choice == "no" then -- Duplicate
      print("Okay goodbye!")
end

:white_check_mark:

function questionChoice()
    if choice == "yes" then
        print("True or False")
        print("Do camels have three sets of eyelids?")
        
        if choice == "true" then
            print("You are... Corect!")
        end
        
        if choice == "false" then
            print("You are... Wrong.")
        end
    elseif choice == "no" then
        print("Okay goodbye!")
    end
end

Here you can clearly see what belongs to which block

2 Likes

You put if instead of elseif when checking if the user’s choice is false. So the code was expecting something to end the check. Here is the updated code:

function questionChoice()
    if choice == "yes" then
        print("True or False")
        print("Do camels have three sets of eyelids?")
        
        if choice == "true" then
            print("You are... Corect!")
        elseif choice == "false" then
            print("You are... Wrong.")
        end
    elseif choice == "no" then
        print("Okay goodbye!")
    end
end
1 Like