"Invalid argument #2 (interval is empty)"

Hi, I have searched on the developer forum regarding this issue and it hasn’t really helped so,
can yall help? (I’m new to metatables)

warn("|--LOADING BACKGROUND MUSIC FRAMEWORK--|")
local musicIds = {
	first = {2900820090, "Robot 64 - Knoddy's Resort"};
	second = {1346962224, "Breakbeat Heartbeat - Constellations"};
	third = {295282153, "Fourside - Super Smash Bros. Melee"};
	fourth = {1834426526, "Pokemon Gold/Silver/Crystal - Azalea Town Theme"};
	fifth = {452214261, "Pokemon Black & White - Nimbasa City"};
	sixth = {939781220, "Sonic Generations - Modern Crisis City"};
}
setmetatable(musicIds, {
	__index = function(key, index)
		warn("|--INDEX HAS NOT BEEN FOUND IN LOCAL MUSIC HANDLER, RETURNING RANDOM FROM TABLE musicIds--|")
		-- THIS LINE vv
		return musicIds[math.random(1, #musicIds)]
		-- THIS LINE ^^
	end
})

while true do
	print("Randomseeding for more randomness...")
	math.randomseed(tick())
	-- THIS LINE vv
	local randomChosenMusic = musicIds[math.random(1, #musicIds)]
	-- THIS LINE ^^
	print("Dialed random music")
	local sound
	print("Created nil instance (Sound)")
	if sound == nil then
		sound = Instance.new("Sound", workspace)
		sound.Name = "Background Music (Local)"
		sound.Volume = 1.3
		print("Created sound")
	end

	sound:Stop()
	sound.SoundId = "rbxassetid://"..tostring(randomChosenMusic[1])
	sound.TimePosition = 0
	sound:Play()
	print("Sound started")
	script.Parent["Song info"].Text = randomChosenMusic[2]
	sound.Ended:Wait()
	print("Sound ended, continuing loop...")
end

Thanks.

The dictionary part of a table does not count towards the length when using the # operator. Instead, make your dictionary an array, so like this:

local musicIds = {
	{2900820090, "Robot 64 - Knoddy's Resort"},
	{1346962224, "Breakbeat Heartbeat - Constellations"},
	{295282153, "Fourside - Super Smash Bros. Melee"},
	{1834426526, "Pokemon Gold/Silver/Crystal - Azalea Town Theme"},
	{452214261, "Pokemon Black & White - Nimbasa City"},
	{939781220, "Sonic Generations - Modern Crisis City"},
}

I am also not sure what the point of this code is:

it will just make debugging confusing in the case of any mistakes. I would personally remove it, since you’re always going to pick a random song. It is also not a good idea to index within __index as this can cause a c stack overflow if the value is nil. That is what rawget is for – to bypass __index.

Thanks, that fixed it.
Oh and to answer your question, I think my brain malfunctioned when I was writing that :sweat_smile:

Music would play again, right?