So I was trying to do something using Object Oriented Programming and created a class car with it’s own methods and properties.
This is my Module
This is how I am creating an object called car and trying to implement a method called start
local carmodule = require(script.Parent["Car Module "])
local mycar = carmodule.new()
mycar:Start()
The error I am getting
`
ServerScriptService.Script:3: attempt to call a nil value - Server - Script:3
`
1 Like
did the Car Module return a table??
Yes the module returned the table Car.
Instead of using function Car:Start()
You have to use function Car.Start()
(the period instead of the colon)
Same goes with the “SetWheels” method.
Hope this helps!
1 Like
Also adding on to this…
mycar:Start()
should be carmodule.Start(mycar)
1 Like
can you tell me the source of the script that is requiring the car module?
1 Like
You forgot to set the index, so Car.__index = Car (in the module) underneath local Car.
1 Like
This is not true since the OP is using OOP when doing Car:Start() the colon is predefined self. Which means it would try to index the metatable, the reason of __index is so that it has a reference to where it should look.
ALL ABOUT OOP!
Prerequisites
An understanding of meta-tables (although the required code will be explained)
How tables work and a competent grasp of the Lua syntax
Parts
What is OOP?
How does it help me?
How do I make this work in Lua?
Integrating with module scripts
What about inheritance?
What is OOP?
OOP stands for Object Orientated Programming and is a way of laying out code in a more friendly way whilst also keeping large projects organised. You have used objects in pro…
1 Like