- What do I want to achieve?
Recently, I have been trying to better learn OOP and Inheritance practices. I have been trying to just create some basic examples based off tutorials to better understand. Currently, I have my BaseClass which contains both properties and methods. I then, have a subclass that inherits some properties from the BaseClass but has its own properties and its own methods.
- What is the issue?
When defining a custom method inside of my subclass, it does not work whenever I try and call it from inside of a ServerScript. Here is the basic layout I have thus far.
ModuleScript titled Car:
local Car = {}
Car.__index = Car
function Car.new(position, driver, model)
return setmetatable({
Position = position,
Driver = driver,
Model = model,
}, Car)
end
function Car:Boost()
print("This vehichle is boosting, VROOM VROOM!")
end
return Car
ModuleScript titled Truck:
local Car = require(script.Parent.Car)
local Truck = {}
Truck.__index = Car
function Truck.new(position, driver, model, slogan)
return setmetatable({
Position = Car.new(position, driver, model).Position,
Driver = Car.new(position, driver, model).Driver,
Model = Car.new(position, driver, model).Model,
Slogan = slogan
}, Truck)
end
function Truck:useSlogan()
print("testing") -- am planning on using the slogan property here.
end
return Truck
The ServerScript:
local Car = require(game.ServerScriptService.Car)
local Truck = require(game.ServerScriptService.Truck)
local carModel = workspace.Car
local truckModel = workspace.Truck
local newCar = Car.new(carModel.Position, "ImTheBestMayne", carModel)
local newTruck = Truck.new(truckModel.Position, "ImTheBestMayne", truckModel, "slogan test")
newCar:Boost()
newTruck:useSlogan()
print(newTruck)
Whenever I call the method useSlogan() inside the ServerScript, that is when I get the title error āattempt to call missing method āuseSloganā of tableā
However, if the method is located inside of the Car MS and not the Truck MS, it works.
Working setup:
local Car = {}
Car.__index = Car
function Car.new(position, driver, model)
return setmetatable({
Position = position,
Driver = driver,
Model = model,
}, Car)
end
function Car:Boost()
print("This vehichle is boosting, VROOM VROOM!")
end
function Car:useSlogan()
print("testing")
end
return Car
- What solutions have I tried thus far?
Really, just reworking the scripts. I originally tried this iteration of the Truck MS.
local Car = require(script.Parent.Car)
local Truck = {}
Truck.__index = Car
function Truck.new(position, driver, model, slogan)
local newTruck = Car.new(position, driver, model)
newTruck.Slogan = slogan
return setmetatable(newTruck, Truck)
end
function Truck:useSlogan()
print("testing")
end
return Truck
My guess as to why this is happening would be the way the indexing is dealt with inside of the Truck MS. However, this confuses me. My understanding was that with .__index is that Lua would first try and find the method inside of the newTruck table. I can see why this would not work because obviously the useSlogan() method is located inside of the truck table. And whenever the method is located inside of Car, the .__index checks that table, in which it finds the method. Well, then what about this line?
return setmetatable(newTruck, Truck)
This would imply that the newTruck table would first check the newTruck table and THEN the truck table, no? So, then why is the method located inside of the Truck script firing an error when called inside a ServerScript?
Any help is appreciated, thanks in advance.