Npcs what values should be managed by the metatable

So I have a simple NPC. I just want to know what values should be managed by the metatables since I have some values that I initially set to the character but I’m not sure if those characterattributes will be better stored in self.

BaseNPC.CharacterAttributes = {
	["Target"] = nil;
	["Stunned"] = false;	
	["StopDistance"] = 5;
	["AttackDistance"] = 7;
	["CanAttack"] = true;
	["AttackCoolDown"] = 0.7;
}

BaseNPC.__index = BaseNPC

--Helper functions

--Functions
function BaseNPC.new(Cframe, Wave)
	local self = setmetatable({}, BaseNPC)	
	self._Model = Properties.model:Clone()
	self._Humanoid = self._Model.Humanoid or self._Model:FindFirstChild("Humanoid")
	self._Animator = self._Humanoid:FindFirstChild("Animator")
	self._Health = Properties.health * (1 + (HealthScale * Wave))
	self._Damage = Properties.damage * (1 + (DamageScale * Wave))
	self._Gold = Properties.gold * (1 + (GoldScale * Wave))
	self._Experience = Properties.experience * (1 + (ExperienceScale * Wave))
	self._WalkSpeed = Properties.speed
	self._Cframe = Cframe or CFrame.new()
	self._Connections = {}
		
	self:Init()
	return self
end

Generally if it’s constant I would put it in the metatable, and if it’s not constant I would not put it in the metatable.

For example, class functions are constant, so those would go in the metatable, but properties (e.g. NPC name, isStunned, etc) should probably not go in the metatable.

You might want to look into “inheritance” if you have a BaseNPC class but then are going to add more NPC types on top of the BaseNPC class. (For example, Part and MeshPart both inherit BasePart.)

Edit:

Here is a reference about inheritance (the link should go directly to the section about it, titled “What about inheritance”):

https://devforum.roblox.com/t/all-about-object-oriented-programming/8585#What%20about%20inheritance?:~:text=change%20if%20needed.-,What%20about%20inheritance%3F,-A%20quick%20explanation

1 Like