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 4271586
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:27:43+00:00 2026-05-21T07:27:43+00:00

New to Lua, trying to figure out how to do OOP using the middleclass

  • 0

New to Lua, trying to figure out how to do OOP using the middleclass library

main.lua:

require 'middleclass'
require 'Person'

local testPerson = Person:new("Sally"); //causes Runtime error: attempt to call method 'new' (a nil value)
testPerson:speak();

Person.lua:

module(..., package.seeall)
require 'middleclass'

Person = class('Person');
function Person:initialize(name)
  self.name = name;
  print("INITIALIZE: " .. self.name);
end

function Person:speak()
  print('Hi, I am ' .. self.name ..'.')
end

Why am I getting that error?

  • 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-21T07:27:44+00:00Added an answer on May 21, 2026 at 7:27 am

    First of all, the semicolons at the end of lines are not necessary and probably a bad habit for writing Lua code. Secondly, I changed require 'middleclass' to require 'middleclass.init' in both files and removed module(..., package.seeall). After that, the example code worked just fine on my machine with Lua 5.1.4.

    main.lua

    require 'Person'
    
    local testPerson = Person:new("Sally")
    testPerson:speak()
    

    Person.lua

    require 'middleclass.init'
    
    Person = class('Person')
    
    function Person:initialize(name)
      self.name = name
      print("INITIALIZE: " .. self.name)
    end
    
    function Person:speak()
      print('Hi, I am ' .. self.name ..'.')
    end
    

    You may be including the middleclass.lua file directly. It is not setup to work that way. The intention is to include middleclass/init.lua.

    If you use the two files exactly as shown above and layout your files as shown below this will work.

    ./main.lua
    ./Person.lua
    ./middleclass/init.lua
    ./middleclass/middleclass.lua
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to wrap a class from a library I'm using in Lua. Specifially,
I'm fairly new to Lua, I've been working on trying to implement Lua scripting
I'm new to the Lua I/O, but have been using Lua elsewhere for almost
I'm trying to embed some Lua scripting functionality in my C# app by using
New to javascript/jquery and having a hard time with using this or $(this) to
New to Linux programming in general. I am trying to communicate with a kernel
The new extensions in .Net 3.5 allow functionality to be split out from interfaces.
In new C++ code, I tend to use the C++ iostream library instead of
I'm using Electro in Lua for some 3D simulations, and I'm running in to
I'm a Lua novice. I'm unit testing Lua 5.1 code using Lunity and LeMock

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.