After learning OOP in Python I came back to Roblox and was able to finally understand how it works however I am still a bit confused mainly with inheritance.
So I currently have this
NPC = {}
NPC.__index = NPC
function NPC.new(Name,Gender,Age,HP,MP)
local NewNPC = {}
setmetatable(NewNPC, NPC)
NewNPC.Name = Name
NewNPC.Gender = Gender
NewNPC.Age = Age
NewNPC.HP = HP
NewNPC.MP = MP
function NPC:Info()
local Pronoun = ""
if self.Gender == "Male" then
Pronoun = "he"
elseif self.Gender == "Female" then
Pronoun = "she"
end
print("This is "..self.Name.." "..Pronoun.." is "..self.Age)
end
function NPC:ChooseClass(Class)
print(self.Class)
self.Class = Class
print(self.Class)
end
return NewNPC
end
return NPC
And then I made a second module called Warrior
NPC = require(game.ReplicatedStorage.NPC)
Warrior = {}
Warrior.__index = Warrior
setmetatable(Warrior, NPC)
function Warrior.new(Name,Gender,Age,HP,MP,PhysicalDEF) --Warrios can have defense against physical atacks
local NewWarrior = NPC.new(Name,Gender,Age,HP,MP)
setmetatable(NewWarrior, Warrior)
NewWarrior.PhysicalDEF = PhysicalDEF
function Warrior:Strike()
print("STRIKE")
end
return NewWarrior
end
return Warrior
And lastly I have my main script
local NPC = require(game.ReplicatedStorage.NPC)
local Warrior = require(game.ReplicatedStorage.Warrior)
--Name, Gender, Age, HP, MP
Person1 = NPC.new("Dom","Male","27",80,120)
Person1:Print()
Now say I wanted this NPC to be a warrior and it to be able to use the strike function how can I do that? Would I need to do Person1 = Warrior.new() instead?