Table[#table + 1] = "string" prints nil

When I use this to try and fire __newindex from a metatable, since table.insert uses rawset, it wont fire the event. So I am trying new methods that will fire newindex. this one should insert the values, but I don’t know why it’s not.

1 Like

Can you show me the code that it doesn’t?

Are you sure?

local t = setmetatable({}, {
	__newindex = print
})

for i = 1, 10 do
	t[#t + 1] = math.random()
end

This works completely fine

I thought adding elements to tables like that didn’t fire the newindex metamethod.

Using table.insert doesn’t, but setting values like that will fire it.

Oh yea yea yea, I got it mixed up sorry.

1 Like

Ill have to try this when I get back, im setting the metatable separately from the table variable. My problem was when i did that, it wouldnt work

im trying to add a singular value, like a player instance
lobby[#lobby + 1] = plr
does not work

1 Like