Recent Change to table.insert causes values at indexes less than 0 to be overwritten

Description of bug:

When you use table.insert and try to insert at index 0, table.insert(table,0,value), it will not add the new value to the list, it will instead overwrite any value at index 0. This happens when trying to insert at any index less than 0. This is not intended behavior and was changed in a recent update. I had to change some code that relied on the length of a table, table.insert(table, #table, value). This code segment would not work.

Where the bug happens:
It happens when you try to table.insert a value at index 0 or less.

When it started happening:
It started happening after the most recent roblox update on 7/7/2021

Screenshots and videos of the bug:
image

Steps to reproduce the issue:
Create a table
table.insert multiple values at index 0
none of them have been inserted
they have all overwritten index 0 instead of being added to the table

2 Likes

This is an intentional behavior change which will be noted in the release notes when they get posted. Before implementing this change we ran extensive analytics and found that almost no games would be affected by this - note that table.insert(t, #t, i) now produces a script analysis warning because this code is incorrect (it’s not just the first insertion that’s problematic, all insertions use off-by-one indices and so you don’t get the correct order).

This change was necessary to fix cases where table.insert with a large negative value or NaN as an index would soft-hang the process.

12 Likes

So what should we do to to not have this affect us? Since it affected it lots of my scripts and they are now broken.

Instead of table.insert(t, #t, v) you should use table.insert(t, v) if that’s the source of the issues for you.

Oh ok thank you for letting me know, but if you don’t mind me asking why did they change it?

You replied to the post with the explanation. (see #2)

3 Likes