Class inheritance issue

I am trying to use classes for my enemies system but I cannot get the code working for the life of me

My problem is that the Monster class is not registering anything from the Skeleton class, so when I set the health, model etc… it doesn’t actually set it

I’ve been changing around and modifying my code for hours and it still doesn’t work, so I’m asking for your help

My current code:

-- Monster.lua

local Monster = {}

Monster.__index = Monster

function Monster.new(model, ...)
    local monster = {}

    setmetatable(monster, Monster)

	local args = {...}

    -- Initialise variables
    for key,value in pairs(args) do
		monster[key] = value
    end
    
	-- Default values
	monster.health = monster.health or 10
	monster.detect_distance = monster.detect_distance or 20
	monster.model = model or monster.model
	
	return monster
end

-- Metamethods...

return Monster
-- Skeleton.lua

local Monster = require(game.ReplicatedStorage.Monster)

local Skeleton = {}

setmetatable(Skeleton, Monster)
Skeleton.__index = Skeleton

function Skeleton.new(Model, ...)
	local skeleton = Monster.new(...)
	
	setmetatable(skeleton, Skeleton)

	skeleton.health = health
	skeleton.detect_distance = detect_distance
    skeleton.model = Model

	skeleton:Initialise(...)

	return skeleton
end

-- More metamethods...

return Skeleton

In the Monster class I have a function I use every frame so I set it up to print self and it returned:

{
    ["Initialise"] = "function",
    ["__index"] = "*** cycle table reference detected ***",
    ["attackTick"] = "function",
    ["idleTick"] = "function",
    ["new"] = "function",
    ["target"] = Puzzled3d,
    ["tick"] = "function"
}

Which is fully Skeleton and no Monster!
And, there should be health, detect_distance and model but they’re nowhere to be seen!

Does anyone know how to make this work? I am extremely stumped

1 Like

Hehe, you’re code is perfectly fine, just turned around. Skeleton is supposed to inherit properties, states, and behaviour of the Monster class.

Simplified Skeleton
local Monster = require(script.Parent.Monster)

local Skeleton = {}
setmetatable(Skeleton, Monster)
Skeleton.__index = Skeleton

function Skeleton.new()
	local skeleton = Monster.new()
	setmetatable(skeleton, Skeleton)

	skeleton.health = 50

	return skeleton
end

return Skeleton
Simplified Monster
local Monster = {}
Monster.__index = Monster

function Monster.new()
	local monster = setmetatable({}, Monster)
	
	monster.health = 10
	monster.detect_distance = 20
	
	return monster
end

function Monster:SpookEveryone()
	warn("BOO")
end

return Monster
Script
local SkeletonModule = require(script.Skeleton)

local Skeleton = SkeletonModule.new()

print(Skeleton.health) --> 50
print(Skeleton.detect_distance) --> 20
print(Skeleton) --> self
Skeleton:SpookEveryone() --> BOO

Think any Roblox instance. It inherits the characteristics of Instance (base) class, just like your Skeleton from Monster.

Update. @Puzzled3d by the way, the loop that stores variadic variables saves them to indices (numerical).

Illustration
local function ConcatStrings(...)
	local args = {...}
	print(args[1])
	return table.concat(args, " ")
end

local function MiddleMan(arg1, ...)
	return ConcatStrings(arg1, ...)
end

print(MiddleMan("Hello", ",", "Beautiful", "World"))

That comes more useful if you know the exact order of optional arguments.

monster.arg1 = varArgs[1]
monster.arg2 = varArgs[2]
2 Likes

How can I use these variables in the Monster class though? When I try reference a variable from the Skeleton class e.g. health / model from within the Monster modulescript it doesn’t have it

1 Like

Monster is supposed to be the base class for other monsters like Skeleton, Zombie, Ghost, and so forth. That is the general idea of inheritance. Monster represents the foundation, while all those subclasses add their own functionality to it. Like vehicles: Vehicle (base class) → Car, Truck … The script requires one of those subclasses, not the base. Another example is the entire system of defualt camera and movement moduels. Just like in the above code snippets.

Having monster with all the functionalities of skeletons, zombies, and others would defy the purpose of inheritance.

2 Likes