Metamethods break after using rawset?

I’ve been messing around with metatables and metamethods. Specifically, I have my own Timer metatable that calls functions after a delay (i know task.delay exists, its unrelated), and I’ve ran into an issue with __newindex.

For some reason, after using rawset, Roblox decides to stop using __newindex. I’m using __newindex so I can start/stop the Timer without calling a specific function.

Is this a bug? It doesn’t sound very intentional that a __newindex stop working after using rawset. Is there a workaround for this?

1 Like

Well, it does what it says on the tin; __newindex. It’ll only be invoked when a new index is created, but after the rawset, the index already exists because of the rawset, so the metamethod isn’t invoked.

1 Like

That’s not right, __newindex is called when you set a value to the index. The name is irrelevant to whether or not the index has a value associated.
Besides, I use a few rawset functions that happen before the specific one that’s causing my issue. I can use something like Timer.Duration = 2 several times to invoke __newindex repeatedly.

__newindex is for setting values, __index is used for getting values.

Also clarifying, I’m not using setmetatable() after the table’s first assigned a metatable

I would like to see how you’re setting your metatable up

Okay, I’m wrong. I’m actually testing it and you’re right, thank you.

I probably remembered it wrong then, I thought I had experiences before where the opposite was the case.

Now I’m wondering how I was able to get __newindex to invoke multiple times…

2 Likes

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