Help with Script to Automate NPC Interactions

Hello everyone,

I am working on a game where players can interact with NPCs and I am looking for help with a script. The goal of the script is to automate the NPC interactions so that they can respond to players’ choices and progress through different dialogues.

I have started writing the script, but I am running into some issues with the NPC responding to players’ choices. Here is the code I have so far:

local dialogues = {
  {
    prompt = "Hi, how can I help you today?",
    responses = {
      { text = "I need information", action = "showInformation" },
      { text = "I have a question", action = "showQuestions" }
    }
  },
  {
    prompt = "What information do you need?",
    responses = {
      { text = "About the game", action = "showGameInformation" },
      { text = "About the NPC", action = "showNPCInformation" }
    }
  },
  ...
}

local currentDialogueIndex = 1

local function showDialogue()
  local currentDialogue = dialogues[currentDialogueIndex]
  print(currentDialogue.prompt)
  for _, response in ipairs(currentDialogue.responses) do
    print(response.text)
  end
end

showDialogue()

The issue I am having is that the NPC does not respond when a player selects a response. I would appreciate any suggestions or solutions to this problem.

Thank you for your time and help!

Best regards,

YhuanCyruzLopez

Instead of using dots, have you tried using brackets? ([ and ]). Every time that I have to find an exact index in a table I use brackets instead of a dot operator. It would be best if you gave it a shot:

print(currentDialogue["prompt"])
print(response["text"]