How to get something from a dictionary

So, i am doing a custom vote system using dictionaries (when you vote, your name gets added alongside what you voted for)
i am currently trying to find out how you will find how many people voted for one particular thing?

i couldnt find anything online that regards to this exact issue, its only things for ‘how many times does this appear in a table’

local function countvalues(dict,v0)
    local num = 0
    for i,v1 in next, dict do
        if v0==v1 then
            num = num+1
    return num

This will work find with small to medium sized dictionaries, but it is better to keep track of votes by combining your solution with a second table that stores the results themselves as indices and their vote counts as values. This way, you completely avoid the need to iterate over anything.

local plyrtab = {}
local votetab = {}

local function vote(plyr,vote)
    local oldvote = plyrtab[plyr]
    if oldvote then
        votetab[oldvote] = votetab[oldvote] -1
    plyrtab[plyr] = vote
    votetab[vote] = (votetab[vote] or 0)+1

it needs to work with this format, as this is how im going to be using it

["Player 1"] = "Sprite",
["Player 2"] = "Map3",
["Player 3"] = "Chicken and Ham sandwich",
["Player 4"] = "StrawBerry"

since it will be more dynamic it wont have fixed values to compare to at any point in time (hence i cant just assign a value to each thing)

The plyrtab dictionary will be storing the values in the format you provided. The other table is only there to check how many people voted for something.

local PlayerVotes = {} 	--stores what the individual player voted for
local VoteCount = {}	--stores the number of votes an individual thing has
local VoteQueue = {}

local SortingVotes = false
local function Vote(plyr,vote)
	VoteQueue[#VoteQueue+1] = {plyr,vote}
	if not SortingVotes then
		SortingVotes = true
			while VoteQueue[1] do
				local VoteInfo = VoteQueue[1]
				local Plyr,Vote = VoteInfo[1],VoteInfo[2]
				local OldVote = PlayerVotes[Plyr]
				if OldVote then
					VoteCount[OldVote] = VoteCount[OldVote]-1
				PlayerVotes[Plyr] = Vote
				VoteCount[Vote] = VoteCount[Vote]+1
			SortingVotes = false

This is a 100% overwrite proof version, though I highly doubt anyone is fast enough to vote within a few CPU cycles from another person.

To retrieve something from the dictionary you provided,

-- How to get something from a dictionary?
-- Assuming your dictionary is as you shared,

local votedictionary = {}
votedictionary ["Player 1"] = "Sprite",
votedictionary ["Player 2"] = "Map3",
votedictionary ["Player 3"] = "Chicken and Ham sandwich",
votedictionary ["Player 4"] = "StrawBerry"

You can retrieve the vote as shown below,

local function get_specific_player_vote(playername)
     return votedictionary[playername]

local player_one_vote = get_specific_player_vote'Player 1' -- This will give you ' Sprite '

Also wrote this simple vote concept script to give you an idea of how things can work alternatively,

-- Quick Vote System Concept

local votes = {} -- The Vote Dictionary

-- All map names

local mapnames = {

-- Assign all map to the vote dictionary

for i,v in pairs(mapnames)do
	votes[v] = {
		Total = 0,
		Players = {}

local function GetPlayerVote(player) -- This function will return what map the player vote for
	for mapname,data in pairs(votes)do
		if data.Players[player] ~= nil then
			return mapname

local function GetWinningVote()
	local highestvote,winning_voted_map = 0,'None'
	for mapname,data in pairs(votes)do
		if data.Total > highestvote then
			highestvote = data.Total
			winning_voted_map = mapname
	if winning_voted_map == 'None' then --  If nobody voted then pick a random map
		winning_voted_map = mapnames[math.random(1,#mapnames)]
	return winning_voted_map
local function insert_vote(player,selected_mapname)
	for mapname,data in pairs(votes)do -- Clear player vote from any existing vote for situations where the player is changing votes
		if data.Players[player] ~= nil then
			data.Players[player] = nil
			data.Total = data.Total - 1
	local votedata = votes[selected_mapname]
	votedata.Total = votedata.Total + 1
	votedata.Players[player] = true

He actually wanted to know how to get the amount of votes something has, not how to access a value in a dictionary.

Aye I provided a source for that in the second code I shared,


the second code is basically how I would do it, though I don’t store votes in a dictionary for reasons, but if I would, that’s how I would manage the dictionary, since in that manner, you only have one dictionary to check for!

local a = {}
a.Amount = 0
a.Players_That_Voted = {}

^ That’s the dictionary concept used :+1:


Oh, sorry didn’t read the code till end.

1 Like