Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5964217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:23:29+00:00 2026-05-22T19:23:29+00:00

Hey guys, just wondering why ain’t my menu working, I’ve been coding it for

  • 0

Hey guys, just wondering why ain’t my menu working, I’ve been coding it for like 8 hours now and just can’t figure out what’s wrong.

Menu = {
    label = "Mahin Menu",
    current = current or true,
    open = open or true,
    subMenus = {}
}

function Menu.newSubMenu()
    return {
        setup = Menu.setup,
        print = Menu.print,
        toggleOpen = Menu.toggleOpen,
        getCurrentMenu = Menu.getCurrentMenu,
        getLastMenu = Menu.getLastMenu,
        getNextMenu = Menu.getNextMenu,
        getPrevMenu = Menu.getPrevMenu
    }
end

function Menu:setup(m_parent, m_label, m_action)
    self.parent = m_parent
    self.label = m_label
    self.action = m_action
    self.subMenus = {}
    self.current = false
    self.open = false
    table.insert(m_parent.subMenus, self)
end

function Menu:print(indent)
    io.write(string.rep("    ", indent))
    if #self.subMenus>0 then
        if self.open == true then
            io.write("[-]")
        else
            io.write("[+]")
        end
    else
        io.write("   ")
    end
    if self.current == true then
        io.write("<" .. self.label .. ">")
    else
        io.write(" " .. self.label)
    end
    io.write("\n")
    if #self.subMenus>0 and self.open == true then
        for i=1,#self.subMenus do
            self.subMenus[i]:print(indent+1)
        end
    end
end

function Menu:toggleOpen()
    if self.open == true then
        self.open = false
    else
        self.open = true
    end
end

function Menu:getCurrentMenu()
    if self.current == true then
        return self
    else
        for k=1,#self.subMenus do
            local v = self.subMenus[k]:getCurrentMenu()
            if v ~= nil then
                return v
            end
        end
    end
end

function Menu:getLastMenu()
    if self.open == true and #self.subMenus > 0 then
        return self.subMenus[#self.subMenus]:getLastMenu()
    else
        return self
    end
end

function Menu:getNextMenu(bool)
    bool = bool or false
    if bool == false then
        if #self.subMenus > 0 and self.open == true then
            return self.subMenus[1]
        end
    end
    if self.parent then
        if self.parent.subMenus[#self.parent.subMenus] == self then
            self.parent:getNextMenu(true)
        else
            for i=1,#self.parent.subMenus do
                if self.parent.subMenus[i] == self then
                    print(self.parent.subMenus[i+1].label)
                    return self.parent.subMenus[i+1]
                end
            end
        end
    else
        return self
    end
end

function Menu:getPrevMenu()
    if self.parent then
        for k=1,#self.parent.subMenus do
            if self.parent.subMenus[k] == self then
                if k == 1 then
                    return self.parent
                elseif #self.parent.subMenus[k-1].subMenus > 0 and self.parent.subMenus[k-1].open == true then
                    local x = self.parent.subMenus[k-1]
                    while #x.subMenus > 0 and x.open == true do
                        x = x.subMenus[#x.subMenus]
                    end
                    return x
                else
                    return self.parent.subMenus[k-1]
                end
            end
        end
    else
        return self
    end
end

Test = Menu.newSubMenu()
Test:setup(Menu, "Test item")
Mahi = Menu.newSubMenu()
Mahi:setup(Menu, "Mahi item")
Mahi.open = true
Testx = Menu.newSubMenu()
Testx:setup(Mahi, "Lalall")
Testx.open= true
Sadmad = Menu.newSubMenu()
Sadmad:setup(Testx, "Woot")
Asd = Menu.newSubMenu()
Asd:setup(Menu, "Asd menu")
Asd.current = true
Menu.current = false

repeat
    print(string.rep("\n",2))
    Menu:print(0)
    x=io.read()
    if x == "z" then
        x = Menu:getCurrentMenu()
        print(Menu:getCurrentMenu().label)
        print(Menu:getCurrentMenu():getNextMenu().label)
        y = Menu:getCurrentMenu():getNextMenu()
        x.current = false
        y.current = true
    elseif x == "a" then
        x = Menu:getCurrentMenu()
        y = Menu:getCurrentMenu():getPrevMenu()
        x.current = false
        y.current = true
    end
until x == "sad"

”
there’s the code, and when ever i try to move my current from “Asd menu” downwards, it’ll error:

menu.lua:150: attempt to index a nil value

which doesn’t make any sense, it’s clearly declared, and I’ve tried adding prints and they always give me Asd menu O.o
Same goes for if I’ll try to move from Woot to Asd menu, same exact error, and I have no idea why, since I added those prints

print(Menu:getCurrentMenu().label)
print(Menu:getCurrentMenu():getNextMenu().label)

and they do give me Asd menu, but then it says that trying to index a nil value at the second print line, but it sill does print? I’m out of ideas, any help out here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T19:23:30+00:00Added an answer on May 22, 2026 at 7:23 pm

    You are missing a return statement in line 92.

    Note that this line does not actually return anything, so the function is returning nil.
    After changing it to return self.parent:getNextMenu(true) it seems to be working.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey guys, ive been working on this drop down for wayyy to long now.
Hey guys, I'm working on a status-Updater. It works, there is just one problem,
hey guys, how can I just achieve this simple layout? I'm hoping the answer
Hey guys. Just wondering if anyone knows of a method to create sparkline graphs
Hey guys, just wondering if their is a simple way to create an Item
Hey guys - I'm stuck with this problem and im just wondering what the
Hey guys, I'm wondering can someone help me out. I'm using javascript to serialize
Hey guys I installed cygwin on my windows 7 just now With ALL THE
Hey guys, just wondering if this one is possible I have an app that
Hey guys! I've been working on c++ application lately which has to be run

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.