Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Is this a good way of using functions in metatables?

Hello, i mostly know how metatables work, although i don't know if when making a module, is this a good way to store __index function?

local module = {
	__newindex = function(a1, a2, a3) -- The function is stored in table, that is returned
		print(a1[1], a2)
	end
}

function module.CreateSetup()
	local statsToReplicate = {}
	
	return setmetatable(statsToReplicate, module)
end

or should i create it like this, store the function in seperate table:

local module = {}

local metatable = {
	__newindex = function(a1, a2, a3)
		print(a1[1], a2)
	end
}

...
return setmetatable(statsToReplicate, metatable)

Thanks, kirda

1 answer

✓ Accepted answer

You can reuse the table that stores your functions to also act as your mt which is fine, but not if you are gonna loop over that module then you will also loop over your metemethods, btw i think you meant to return the module in the second example bc now you dont even use that table and require will always return the same statstoreplicate. It all depends on your preferences it doesnt make a big difference

1 comment

0

okay thank you

This discussion is preserved as it was. New answers, comments, and votes are closed.