How do i link a function to a table with __newindex while still making it persist in the actual table

iwas tryna do a thing where if a new index is added to a table, do something then actually add it because if you try to do it with __newindex it just doenst do that
i used __newindex to do that but i still need the added item to PERSIST in the table so it actually gets added

when i try to do that it just gives me “C stack overflow” from what i heard its when like you try adding too many things to a table that it overflows memory so it just throws out that as in panic, which is like usually done when you do a loop where like basically youre calling the function in itself, so it enters a loop of “i got called, i should call myself again, i called myself, i will call myself again” so it just never ends

what can i do or do i just have to resort to another alternative

5 Likes

A C stack overflow in Roblox Lua means the call stack limit was exceeded due to infinite or uncontrolled recursion. It does not mean memory overflow from too many table entries.

In your case, it happens because your __newindex metamethod is assigning back into the same table in a way that triggers __newindex again. That creates this loop.

Your understanding about recursion causing it is correct. The memory explanation is not.

:woozy_face::fearful::sparkles::balloon: A few emojis so you know I’m not a jerk. Just straightforward. :rofl:

That “In a way” was the pressed[key] .. Forums is a hard game.

5 Likes

stack overflow is when the stack gets too high. basically, infinite function recursion

use rawset instead

1 Like

When you do pressed[key] = val inside the __newindex function, it calls itself again forever → C stack overflow. Try use rawset to actually add the value without triggering the metatable.

2 Likes

forgot to change the title when editing whoops

1 Like

bro just said what i said