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

Is there a MetaMethod that runs when a Variable in a Table is manipulated?

So getting something like this to work:

local tab = {
b = 2
}

local m = {
__SomeMetaMethod = function()
	print("Ran")
end
}

local sm = setmetatable(tab1,m) 

tab.b = tab.b + 1

So upon the variable "b" being manipulated, I want the MetaMethod to run and print "Ran". I've looked through the ROBLOX Wiki on MetaTables, but as someone who is new to MetaTables didn't catch anything that would work. Any ideas / knowledge? All answers appreciated.

2 comments

0

I don't think so, but there should be :(. The closest thing is __newindex

0

I know; I was using that before but then realized I would need an a function that also manipulates variables :/

1 answer

No

The problem here is that you're looking at three different metamethods, applied to two different objects. This is in an imaginary world where tables' metamethods act just like userdata's metamethods for some absurd reason.

  • First, your tab.__index is invoked to get b. You now know that b was accessed from tab
  • Then, your b.__add is invoked to make b+1. You now know that b, accessed from tab, was manipulated.
  • And finally, your tab.__newindex is invoked to set tab.b = b+1. You now know that b, accessed from tab, was manipulated and set back to tab.b again.

I'm sure by now you're seeing the issue - Tracking these sorts of things becomes something of an impracticality, because it involves keeping values as stateful objects rather than simply meaningful values. And that sort of abstraction is cool when you get to mess with it, but mirthlessly useless if you're using it in your own code.

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