Table.insert is inconsistant

I have a piece of code which will insert things into a table based on two numbers inside of a module script, however whenever I try to put something in the 4th place, suddenly it breaks and causes extra nil values specifically in index 2 or 3 at random. It is running on a local script.

repeat
task.wait()
until game:IsLoaded()

Players = game:GetService(“Players”)
player = Players.LocalPlayer
gui = player.PlayerGui.Menu
guiScroll = gui.Frame.SongScroll
ReplicatedStorage = game:GetService(“ReplicatedStorage”)
ReplMisc = ReplicatedStorage.Misc
songButton = ReplMisc.SongButton
events = ReplicatedStorage.Events

menuFolder = workspace.Menu
selectFolder = menuFolder.SongSelect

songList = {

}

buttonDist = 0.06

–gui.Enabled = true

local start = selectFolder.Start:WaitForChild(“Ground”)
local startingPoint = start

for i,chart in ipairs(ReplicatedStorage.Charts:GetChildren()) do

local chartModule = require(chart)


if songList[chartModule.setlist] == nil then
	songList[chartModule.setlist] = {}
end


table.insert(songList[chartModule.setlist],chartModule.position,chart)

end

4 songs in each setlist, when I remove the 4th song in each it works fine, and when I change the position in the 4th song to 5 it also works fine. I am so confused.

output that’s “fixed” (when chartModule.position = 4):
image

weird output (when chartModule.position = 4):
image

To correctly insert multiple values into a table, I advise you insert them as a single table (i.e., a dictionary) containing both values. This approach ensures that each item in the items table is a dictionary with price and name keys, making the data structure more organized and accessible.

Here’s how you can modify the code:

table.insert(items, {price = v.price.Value, name = v.Name})

Alternatively, you can use direct assignment, which is more efficient:

items[#items + 1] = {price = v.price.Value, name = v.Name}

This method appends a new dictionary to the items table, where each dictionary contains the price and name of an item.

Additional Recommendations:

  • Avoid Using # on Non-Array Tables: The # operator is intended for array-like tables (tables with consecutive integer keys starting from 1). Using it on tables with custom keys (like dictionaries) can lead to inaccurate results. Instead, use table.insert() or direct assignment as shown above.

  • Data Structure Design: Consider using dictionaries for storing items, as they allow for more flexible and readable code. For example:

local items = {
    [1] = {name = "Item1", price = 100},
    [2] = {name = "Item2", price = 200},
    -- ...

This structure makes it easier to access and manipulate item data.

  • Data Migration: If you need to migrate existing data stored in a different format (e.g., a list of values) to a new structure (e.g., a list of dictionaries), you can iterate over the old data and construct the new format accordingly.

By implementing these changes, you should be able to resolve the issues with table.insert() and improve the reliability and clarity of your code.

Please do let me know if any of what I had listed will help your case.

Also please instead of putting #help-and-feedback:building-support, put #help-and-feedback:scripting-support, as this is scripting, not building.

okay so,

songList[chartModule.setlist][chartModule.position] = chart

accomplishes the same goal, I can’t believe I didn’t just do this from the start.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.