I’m trying to use table.insert to add values to a pre-made table.
local drops = {
[3] = "Sword",
[6] = "Sword +1",
[7] = "Shield"
}
for i = 1, 2 do
table.insert(drops, i, "Gold")
end
for i = 4, 5 do
table.insert(drops, i, "Gold")
end
print(drops)
However, any index before the pre-made index is being removed (ex table.insert(drops, 2, “Gold”) will remove [3] = “Sword”) and index 6 and 7 are being shifted up by 1, leaving index [3] and [6] “void”, or nil.
This is what the output shows:
and can be tested using this code:
local test = {
[3] = "Three"
}
table.insert(test, 2, "Two")
print(test) -- [3] = "Three" is suddenly missing
I’ve already thought about using table[index] = value instead as shown below which works properly, but I was wondering if this is a problem with my coding, roblox engine, or the function itself. Can anyone else get the same result, or give me insight on this? If this is really a bug, then could someone else report this issue because I do not have permission to post there.
local drops = {
[3] = "Sword",
[6] = "Sword +1",
[7] = "Shield"
}
for i = 1, 2 do
drops[i] = "Gold"
end
for i = 4, 5 do
drops[i] = "Gold"
end
print(drops)