How to get total votes

-- Server
local function VoteCast(player, vote)
	Votes[player.UserId] = vote
	
	GetVotes:FireAllClients(Votes)	
end

-- Client
local function ChangeVotes(votes)
	for i, v in pairs(votes) do
        print(i, v) -- player, their Vote
	end
end

GetVotes.OnClientEvent:Connect(ChangeVotes)

UI is setup like so

Choices
    1
        Gamemode -- Strigng Value
        Votes -- TextLabel
    2
        Gamemode -- Strigng Value
        Votes -- TextLabel
    3
        Gamemode -- Strigng Value
        Votes -- TextLabel

So I pick 3 random gamemodes and assign them each a UI slot. Problem I now have is trying to add up those votes to show the client. Because the vote table would look like this

Player1 = 'Gamemode1',
Player2 = 'Gamemode1',
Player3 = 'Gamemode3',

So Gamemode 1 has two votes, Gamemode 2 has no votes and gamemode 3 has 1 vote. How can I get that and imlement into the GUI?

2 Likes

You could make a separate table for the votes.

local mode_votes =
{
    Gamemode1 = 0,
    Gamemode2 = 0,
    Gamemode3 = 0
} 

then from your VoteCast function

mode_votes[vote] = mode_votes[vote] + 1

1 Like

How would I determine the gamemodes tho?? 3 gamemodes are picked at random, so they aren’t always the same

Oh so it’s not only three? So three random ones are picked at once? In that case

local mode_votes =
{
    [Gamemode1] = 0,
    [Gamemode2] = 0,
    [Gamemode3] = 0
}

Assuming they’re variables this can work.

How would I do this tho? Let’s say the gamemodes are TDM, CTF, KOTH, LMS. It picks 3 at random.

Then players vote, and the votes table looks like so

-- Let's say their 3 choices are TDM, KOTH and CTF
Player1 = 'TDM',
Player2 = 'TDM',
Player3 = 'KOTH',

2 votes for TDM
0 votes for CTF
1 vote for KOTH

Do you think you can give me a little more code, specifically how you pick the game modes, so I can work it out.

Don’t think it really matters in this case

local function GetRandomGamemodes()
	local AllGamemodes = {}
	
	ChosenGamemodes = {}
	
	for _, v in pairs(script:GetChildren()) do
		table.insert(AllGamemodes, v.Name)
	end
	
	for i = 1, 3 do
		local RandomGamemode = math.random(1, #AllGamemodes)
		ChosenGamemodes[i] = AllGamemodes[RandomGamemode]
		table.remove(AllGamemodes, RandomGamemode)
	end
		
	ChooseGamemode:FireAllClients(ChosenGamemodes)
end

I’m trying to grasp your concern so please bear with me if I’m not fully understanding what you’re getting at here.

After selecting all of the random game-modes for that round, I would place them all into a table and have them defined as 0, being the starter amount of votes.

local Voting = {
	["Gamemodes"] = {
		["TDM"] = 0,
		["KOTH"] = 0,
		["CTF"] = 0
	},
	["Players"] = {
		[35238060] = {true, "TDM"}
	}		
}

I would check if the player has voted, and if they haven’t, then you’ll add them to the players table and set the fact that they’ve voted to true. You would also add a vote to the gamemodes table. If you plan on implementing vote changing, then you can simply check Voting["Players"][Player.UserId][1] == true, and if it is, then you can remove a vote from the gamemode located in the player’s table and add one for the new gamemode that they voted for.

Not sure if this is the best way of going at this, or if this solves your problem at all, but it seems reasonable enough and was one of the ways I could currently think of.

3 Likes