I have a table as a dictionary and I am trying to insert multiple music IDs into it for later use, however only one ID is being inserted and I have no idea why.
local music = {}
for i,v in ipairs(workspace.MapsArchive:GetChildren()) do
local musicID = v:WaitForChild("Settings"):WaitForChild("Music")
music[v.Name] = musicID.Value
end
print(music)
All that’s being outputted is this:
{
["TremaineTranquility"] = 1836640373
}
Here’s the heritage of the maps, they are the direct child of workspace:
The issue is that table.insert always inserts a numerical value. It’s easier to just do local musicid = music.MapName than assign numbers to each maps, especially if I want to add more maps in the future.
ipairs will stop at a nil value, how a nil value is being discovered I’m not sure so this may not be the issue, but nevertheless perhaps change this from an ipairs to a pairs loop for testing purposes?
I’ve attempted it myself replicating your system and it works fine for me which leads me to believe it is a loading latency issue:
local music = {}
for _,model in ipairs(game.ServerStorage.Folder:GetChildren()) do
musicId = model:WaitForChild("Music")
music[model.Name] = musicId.Value
end
for i,v in pairs(music) do
print(i,v)
end
Output:
Explorer:
If this is a matter of loading, perhaps you could add a wait() prior to the loop so that everything has the time to load in.
@loveicicIe there is nothing wrong with him using a dictionary as opposed to an array or a table in this situation, there’s no need to advise him against using something that isn’t harming his script in anyway (and the transition wouldn’t benefit his script either).
ok so your table work, but i think @lmp_erator have a good point using ipairs when dealing with table, so if you want to play all music do the table.foreach, and if you want to play a specific one, do music[Maps Name]