Is it possible to have a table of dictionaries?

I’m making a game with my friend and I tried to have a table consisting of multiple dictionaries. However, when I called a random dictionary in that table, it said that there were no valid items in that table.

I’m not very skilled with using dictionaries, but is it really possible to have a table with dictionaries inside of them?

I don’t want to leak the code unless absolutely necessary.

Do you mean something like this?

local tables = {
    {
         index = "value"
    },


    {
         index = "another value"
    }
}

Well, that’s kind of needed to help your specific situation; hiding the code can’t help up see where the issue is

Yes, you can have a table with dictionaries inside it. The problem is probably with the way you were choosing a random value, you can do the following.

local MyTable = {
	MyArray = {
		1,
		2,
		3
	},
	MyDictionary = {
		["1"] = 1,
		["2"] = 2,
		["3"] = 3
	},
	MyOtherDictionary = {
		["3"] = 3,
		["2"] = 2,
		["1"] = 1
	}
}

local RandomEntry = MyTable[math.random(#MyTable)]

ok heres the script

local status = game.ReplicatedStorage.StatusValue

local minigames = {
	--format: *game name* = display game name, serverstoragemodel name, instructions, time
	CliffClimb = {
		DisplayName = "Cliff Climb",
		Location = game.ServerStorage:FindFirstChild("CliffClimb"),
		Instructions = "Get to the top of the mountain!",
		instructionstime = 5,
		timelimit = 30
	},
	
}

while true do
	if true then --change to #game.Players:GetChildren() > 0
		for i = 15,1,1 do
			status = "Game starts in " .. i .. " seconds"
			wait(1)
		end
		local chosengame = minigames[math.random(1,#minigames)]
		status.value = "Chosen minigame: " .. chosengame.DisplayName
		local gameclone = chosengame.Location:Clone()
		gameclone.Parent = workspace
		wait(2)
		for i = 3,1,1 do
			status = "Teleporting players in " .. i .. " seconds"
			wait(1)
		end
		status.Value = chosengame.Instructions
		wait(chosengame.instructionstime)
		for i = chosengame.timelimit,0,1 do
			wait(1)
			status = i
		end
		
	else
		status.Value = "Not enough players. Maybe invite some friends?"
	end
	wait()
end

I guess showing this code cant be that bad

edit: the problem with this script is that when I try to call a random value from the minigames table, it says that nothings in there

I have tables of dictionaries of dictionaries. This is object oriented territory…

Troubleshooting the data retrieval sucks, but it’s SO worth it.

Ok, I see the issue.

This only works for array-based tables, not dictionaries.

Change this to:

{
   DisplayName = "Cliff Climb",
   Location = game.ServerStorage:FindFirstChild("CliffClimb"),
   Instructions = "Get to the top of the mountain!",
   instructionstime = 5,
   timelimit = 30
},

So your table should be setup as:

local minigames = {
    {
       DisplayName = "Cliff Climb",
       Location = game.ServerStorage:FindFirstChild("CliffClimb"),
       Instructions = "Get to the top of the mountain!",
       instructionstime = 5,
       timelimit = 30
    },
}

If you do need the index though, you can do:

local function get_random_dictionary_key(dict)
    --loops through the provided dictionary and returns a random value
    local keys = {} -- table to hold the keys of the dictionaries

    for key, _ in pairs(dict) do
       table.insert(keys, key) -- adds the index to the 'keys' table
    end

    return dict[keys[math.random(#keys)]] -- returns a random dictionary value
end
1 Like

Your random code at the bottom wouldn’t work. Length operators do not work on dictionaries because they are not indexed by contiguous numerical values, so the interval in your random would be empty and if it wasn’t the index wouldn’t work because MyTable only has string indices.

You would need to collect MyTable’s indices into a separate table, call random on that and then index MyTable with the given result. I noticed late but the above provides an example of this.

Just thought you might want to know.

1 Like