There any way to use metatables to get a method function and if it doesn't find the desired method in the first object use the parent method?

I’m new to meta tables and I’m kind of lost, but I kind of wanted to use the parent’s desired method in case I don’t find the same in the child, but I don’t know how to start.

I don’t know if what I said was too confusing, but I wanted, for example, a walk() function for a child of a metatable (I don’t know its name yet I’m sorry), and this function will run normally, but if the child doesn’t have the walk() function get the walk() function from the parent.
I would use this to make more detailed classes.

Is there any way to do this with metatables?

Yes, there is. You will benefit from looking at the metamethods page of the DevHub.

The metamethod you’ll use for this will be __index.

local meta = {}
meta.Print = function()
    print("hi")
end
meta.__index = meta

local test = setmetatable({}, meta)
test.Print = function()
    print("hello")
end

local test2 = setmetatable({}, meta)

test2.Print()
test.Print()

The output should be hi, and then hello, respectively. By setting a metatable to a table, if the table does not have the value, it will invoke the __index metamethod of the table that’s linked to it. By setting the __index of the meta table to itself, it’s basically telling the other table: When you can’t find something, look for it in me. Again, you can do a lot with this so I suggest you read on metamethods on the DevHub.

I’m sorry if it’s a bad explanation. I can elaborate further if this doesn’t cut it

Thanks for the explanation NeptuniaI , it helped me a lot to understand this. But I’m having a problem when trying to do this, when trying to call the child’s function, it goes straight to the parent’s even though it exists in the child. Would you know what it could be?

Code:

local SpeedyNPC = require(game.ServerScriptService.NPC.Rapido)
local SpeedyNPC = SpeedyNPC.new(25)

SpeedyNPC:spawn()
SpeedyNPC.Walk()

----------------------------------------------------------------------------------
BaseModule: 
----------------------------------------------------------------------------------
local BaseNPC = {}

BaseNPC.__index = BaseNPC


function BaseNPC.new(Name)
	local new = setmetatable({},BaseNPC)
	new.Body = script:WaitForChild("NPC"):Clone()
	new.Humanoid = new.Body.Humanoid
	new.Name = Name
	new.Speed = 16
	return new
end

function BaseNPC:spawn()
	if self.Body then
		self.Body.Parent = game.Workspace
		self.Body.Name = self.Name
	end
end

BaseNPC.Walk = function()
	print("BaseHi")
end

return BaseNPC
----------------------------------------------------------------------------------
Class Module : 
----------------------------------------------------------------------------------
local BaseNPC = require(script.Parent)

local Classe1 = {}
Classe1.__index = Classe1

function Classe1.new(Speed)
	
	local ClassePai = {}
	ClassePai.__index = BaseNPC.new("Rapido")
	
	local new = setmetatable(ClassePai,ClassePai)
	new.Speed = Speed	
	
	print("test2")
	
	return new
end


Classe1.Walk = function()
	print("HiTest") -- Should be here , and also , theres a way to use Class1:Walk instead of Classe1.Walk = function() to call self?
end


return Classe1
----------------------------------------------------------------------------------

I see you speak portuguese. I’ll be able to help in a few minutes, hang in there

1 Like
local BaseNPC = require(script.Parent)

local Classe1 = {}
Classe1.__index = BaseNPC

function Classe1.new(Speed)

	local ClassePai = {}
	--ClassePai.__index = 
	local lol = BaseNPC.new("Rapido")
	local new = setmetatable(lol,{
		__index = Classe1
	})

	new.Walk()
	new.Speed = Speed	

	print("test2")

	return new
end

function Classe1:Walk()
	print("HiTest")
end



return Classe1

This seems to work. Basically, __index redirects an index to its metatable. By setting Classe1's __index to BaseNPC, it will look for the function in BaseNPC if it doesn’t exist in Classe1. Also, when creating a new object of class Classe1, we need to create a new table and set its __index metamethod to be Classe1.

I have no idea if this is the right or most efficient approach to this so I’d say play with the code, but this is how I solved the issue for myself.

1 Like

I finally got it, I had to fix the index and then in the child’s function I put new.Walk = NpcRapido.Walk, to replace the current function. Thank you for your help! :sunglasses::+1:

1 Like