Implementing Metamethods in a LocalScript from a ModuleScript

Im trying to do getmetatable on a local script and further implement the metamethod functions into the localscript from a modulescript, why you might ask, its cuz im trying to get the __newindex function to be compatiable to a function at my local script so it becomes like .Changed event from the Modulescript,

The Modulescript:

m = {}
m_metafunctions = {}
setmetatable(m,m_metafunctions)
return m

Local Script:

local m = require(game.ReplicatedStorage.Movement)
local m_metafunctions = getmetatable(m)
m_metafunctions.__newindex = function(table,key,value)
   print(table,key,value)
end

I have tested it and checked for any spelling errors but there aint none, the output seems to print out nothing

1 Like

You can’t get or set the metatable of instances. Did you mean to require the instance?

you forgot to require the module

local m = require(game.ReplicatedStorage.Movement)

the rest should work

my fault i wrote wrong here on the devforum, in the script it has the required function aleready

sorry my fault wrote wrong on the devforum, in the local script there is a require function aleready

You can’t edit tables through a module require, you need to edit it within the module. A way you could do it is by making a function in the module that takes a name and a function, and then edits the metatable like so:

local module = {}

local module_metatable = {}
setmetatable(module, module_metatable)

function module.UpdateMetatable(method, func)
    module_metatable[method] = func
end

return module

Then you can use the UpdateMetatable function in the local script. (Make sure the passed ‘method’ is a string, e.g. “__newindex”)

Are you adding new keys to the table or setting existing ones?

no i just kinda want .changed property at a modulescript which local script reacts at, btw @M_dgettMann got the answer for me

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.