Basic issues with a detecting a value in a table

Anyone able to tell me why when I use Votes[MapLocation.Name] = MapLocation.Info.Votes.Value does not show up when I use #Votes? It shows as 0 when there should be 3 values.

local function LoadMap()
        local Votes = {}
        for i,v in pairs(SelectedMaps) do
            local MapLocation = ServerStorage.Maps.GameMaps:FindFirstChild(SelectedMaps[i])
            Votes[MapLocation.Name] = MapLocation.Info.Votes.Value
        end
        task.wait(0.1)
        print(Votes)
        local Selection
        for i,v in pairs(Votes) do
            print(i, type(i), v, type(v))
            print(#Votes)
        end
        if Votes[1] == 0 and Votes[2] == 0 and Votes[3] == 0 then
            Selection = SelectedMaps[math.random(1,#SelectedMaps)]
        else
            table.sort(Votes, function(i,v)
                return (i[2]>v[2])
            end)
            print(Votes)
            Selection = Votes[1]
        end
        print(Votes)
        print(Selection)
    end```
1 Like

There is nothing in the table Votes, how do you find “MapLocation.Name” from the table Votes…?

It isn’t trying to search for it. It appends a new value but I am an idiot and was treating the dictionary like a table. I resolved it by converting it to a table.

2 Likes

Dictionaries are tables, similarly, arrays are tables (which is what you’re referring to). The unary length operator which is denoted by the hash key “#” is only compatible with table values which follow an array like structure, it is incompatible with table values which follow a dictionary like structure. Here’s a function I wrote earlier which can get the length of a dictionary.

local function dictionaryLength(dictionary)
	local count = 0
	for key, value in next, dictionary do
		count += 1
		if not next(dictionary, key) then
			return count
		end
	end
end