So, I’m coming back to coding on Roblox and I’m trying to understand OOP and inheritance. Using an older tutorial ( All about Object Oriented Programming )
To my understanding, I should be able to have a Class called " Tool " which has its own properties and functions and by using inheritance I can inherit all those properties and functions + assign more to my subclass?
THE PROBLEM
I cannot seem to get the functions ( methods? ) of my subclass to actually show up. I can use functions from the main class ( tool ) but cannot use or reference any from the subclass ( pickaxe ) ?
Tool = {}
Tool.__index = Tool
function Tool.new(owner, durability, damage)
local NewTool = {}
setmetatable(NewTool, Tool)
NewTool.Owner = owner
NewTool.Durabiltiy = durability
NewTool.Damage = damage
return NewTool
end
function Tool:UseTool()
print(5)
end
function Tool:ChangeValue(val)
self.Owner = val
print(self.Owner)
end
return Tool
I am pretty certain this is right, I’ve messed around with it a little and it seems to work.
local CreateTool = require(script.Parent)
local Pickaxe = {}
setmetatable(Pickaxe, CreateTool)
Pickaxe.__index = Pickaxe
function Pickaxe.new(owner, durability, damage)
local NewPickaxe = {}
setmetatable(NewPickaxe, Pickaxe)
return NewPickaxe
end
function Pickaxe:CreatePart()
print(3)
end
function Pickaxe:TestPart()
print(4)
end
return Pickaxe
:CreatePart and :TestPart do not work, only :UseTool and :ChangeValue
This is where the trouble is I imagine, I’ve changed return NewPickaxe to return Pickaxe which has the effect of allowing me to use all the functions, but then this would cause other issues I’d imagine.
local Pickaxe = require(...)
NewPick = Pickaxe.new()
NewPick:FUNCTIONHERE
Where FUNCTIONHERE is, I only get functions from the main class and not from the pickaxe subclass
Edit: I should also mention, when trying to call one of those functions from the subclass I get the following error: " attempt to call missing method ‘TestPart’ of table "