What's the difference with table.insert and changing it than changing it without table.insert?

What’s the difference between a table.insert then changing it to a value than changing it without table.insert?

Before I would expect it like this:

local tb = {
  ["Oof"] = "Oo1f",
}

table.insert(tb, "LOL")
tb["LOL"] = "oo"

for I,v in pairs(tb) do
  print(i,v)
end

-- Output:
-- 
-- Oof Oo1f
-- LOL oo

Then, now it would be:

-- Oof Oo1f
-- 1 LOL
-- LOL oo

In script, we now put without using table.insert to change the value once it’s inserted:

tb["LOL"] = "oo"

The difference is table.insert only inserts to the array part of the table and uses rawset to bypass __newindex where as doing it by hand may invoke __newindex

1 Like