So, I'm new to OOP in Lua - but if I use it, my game will be very efficient. However, I'm quite confused on how to use it for my NPCs. Basically, I just want to know if I'm understanding what I'm doing. I have a module that will construct a mob class for every mob I create.

The code is:

local Mob = {}

function Mob.new(Self)
	local Entity = {Self}
	setmetatable(Entity, Mob)
	return Entity
end

function Mob:Spawn()
	print("Hi")
end

function Mob:Initialize()
	self:Spawn()
end

return Mob

--Insert methods here that manipulate the model

Self is the model of the mob that I want to reference to. So when I spawn a mob via an external script, it'd have the new Mob object reference to the model to perform its methods. For example:

--Insert pseudocode here to grab module, let's name the module "Constructor"
--And we have a mob model already defined and all
Constructor.new(Model)
Constructor:Initialize()

However, it states initialize is a not a method of the new object. Why is this happening?

4 comments

0

Once run on a script, require will always return the same result, which is then stored.

0

Noting that, is the initial function correct for creating the object?

0

Normally a constructor does not need a pre-existing object (unless its a copy constructor), so Mob.new() would make more sense.

0

The issue is that I can't seem to clone mobs from ServerStorage/ReplicatedStorage as they'll lose their baseparts. Any alternatives?