Help with tables!

Hey guys, I am attempting to make a daily objectives system that randomly loops through a table containing other tables to decide what the daily objective is going to be. I keep receiving this error: “ServerScriptService.Scripts.DailyObjectives:91: invalid argument #2 to ‘random’ (interval is empty)”

Could someone please tell me where I may be going wrong?
Thank you!

local objectivesTable = {


	ZombieKillsObjective = {

		Objective1 = {

			Description = "Kill 75 Zombies",
			Type = "ZombieKills",
			Amount = 75,
			RewardXp = 250,

		},

		Objective2 = {

			Description = "Kill 150 Zombies",
			Type = "ZombieKills",
			Amount = 150,
			RewardXp = 500,

		},

		Objective3 = {

			Description = "Kill 50 Zombies",
			Type = "ZombieKills",
			Amount = 50,
			RewardXp = 150,

		},
	},

	PlayTimeObjective = {

		Objective1 = {

			Description = "Play for 15 Minutes",
			Type = "Time",
			Amount = 900,
			RewardXp = 100,			

		},

		Objective2 = {

			Description = "Play for 30 Minutes",
			Type = "Time",
			Amount = 1800,
			RewardXp = 250,			

		},	

		Objective3 = {

			Description = "Play for 45 Minutes",
			Type = "Time",
			Amount = 2700,
			RewardXp = 400,			

		}		


	},

	GameObjectives = {


		Objective1 = {

			Description = "Survive to Round 10",
			Type = "Round",
			Amount = 10,
			RewardXp = 250,			

		}		



	}


}


local DailyTask1Index = math.random(1,#objectivesTable.ZombieKillsObjective) -- This is returning an error

print(DailyTask1Index)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

Unfortunately, you can’t get the length of a dictionary using #. Here is a function that can find the lenth of a dictionary: https://devforum.roblox.com/t/usage-of-len-to-get-length-of-kv-dictionary/473745/2?u=maxximuspower

1 Like

Just to provide some additional context, that error occurs when math.random's second argument is greater than its first, in this case math.random is being passed a second argument of 0. The length of dictionaries is 0.

1 Like

I’d personally fix this by converting dictionaries into arrays.

ZombieKillsObjective = 	{
	{
		Description = "Kill 75 Zombies",
		Type = "ZombieKills",
		Amount = 75,
		RewardXp = 250,
	},

	{
		Description = "Kill 50 Zombies",
		Type = "ZombieKills",
		Amount = 50,
		RewardXp = 150,
	},

	{
		Description = "Kill 150 Zombies",
		Type = "ZombieKills",
		Amount = 150,
		RewardXp = 500,
	},
}
1 Like

Thank you! This is what I was looking for.