Making a map voting

I’m making a map voting system and the code it supposed to print out the map with the most votes but it always prints out “Red Map” even though I didn’t vote for it.

I put this code inside every part (className is part)

local playerTable = {}
local FinishedVotingModule = require(game.ServerScriptService.FinishedVoting)


script.Parent.Touched:Connect(function(hit)
	local player = hit.Parent
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid then
		local RepStore = game:GetService("ReplicatedStorage")
		if table.find(FinishedVotingModule, hit.Parent.Name) then
			print("You have already voted")
		else
			RepStore.VotesGreen.Value = RepStore.VotesGreen.Value + 1
			table.insert(playerTable, hit.Parent.Name)
			table.insert(FinishedVotingModule, hit.Parent.Name)
		end
	end
end)

while wait(5) do
	table.clear(FinishedVotingModule)
	game.ReplicatedStorage.VotesGreen.Value = 0
end

Every script adds to it’s own values and this is an example for a green part.

Inside the server script service

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local VotesRed = ReplicatedStorage.VotesRed
local VotesBlue = ReplicatedStorage.VotesBlue
local VotesGreen = ReplicatedStorage.VotesGreen
local InLobby = ReplicatedStorage.InLobby

local mapTable = {VotesRed.Value, VotesGreen.Value, VotesBlue.Value}

repeat
	
	table.sort(mapTable)
	if mapTable[1] == VotesRed.Value then
		print("Red Map")
	elseif mapTable[1] == VotesBlue.Value then
		print("Blue Map")
	elseif mapTable[1] == VotesGreen.Value then
		print("Green Map")
	end
	
until InLobby.Value == false

image

My output is red map even if I vote for another map.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local VotesRed = ReplicatedStorage.VotesRed
local VotesBlue = ReplicatedStorage.VotesBlue
local VotesGreen = ReplicatedStorage.VotesGreen
local InLobby = ReplicatedStorage.InLobby

local mapTable = {
    ["Red"] = VotesRed.Value,
    ["Green"] = VotesGreen.Value,
    ["Blue"] = VotesBlue.Value
}

function GetMap(Table)
    local function GetTable(Map, Votes)
        return {
            ["Map"] = Map,
            ["Votes"] = Votes
        }
    end

    local MostVotes = {GetTable("", 0)}

    for Map, Votes in pairs(Table) do
        if Votes == MostVotes[1].Votes then
            table.insert(MostVotes, GetTable(Map, Votes))
        elseif Votes > MostVotes[1].Votes then
            MostVotes = {GetTable(Map, Votes)}
        end
    end

    local ChosenMap = Random.new():NextInteger(1, #MostVotes)
    local Map = MostVotes[ChosenMap].Map
    local Votes = MostVotes[ChosenMap].Votes

    return Map, Votes
end

repeat
    local Map, Votes = GetMap(mapTable)
    print(Map, Votes)
until InLobby.Value == false

this should even work if some maps have the same number of votes, try it out

EDIT: improved code

It’s only printing a map name when I run it and after that it just stops printing the map name.

I updated the code btw, does it still do that?
I tested it and it works fine

It doesn’t print anything after the first time.
image

It picked randomly in the first time.

then InLobby.Value must be false, therefore stopping the loop

EDIT:

proof it works


I tried this on Lua: demo and it worked

code I tested on lua demo
local mapTable = {
    ["Red"] = 3,
    ["Green"] = 7,
    ["Blue"] = 5
}

function GetMap(Table)
    local function GetTable(Map, Votes)
        return {
            ["Map"] = Map,
            ["Votes"] = Votes
        }
    end

    local MostVotes = {GetTable("", 0)}

    for Map, Votes in pairs(Table) do
        if Votes == MostVotes[1].Votes then
            table.insert(MostVotes, GetTable(Map, Votes))
        elseif Votes > MostVotes[1].Votes then
            MostVotes = {GetTable(Map, Votes)}
        end
    end

    local ChosenMap = math.random(1, #MostVotes)
    local Map = MostVotes[ChosenMap].Map
    local Votes = MostVotes[ChosenMap].Votes

    return Map, Votes
end

local Map, Votes = GetMap(mapTable)
print(Map, Votes)

I figured out the solution for constant selection (I forgot to change inLobby value to true) but now it’s just picking random maps.

so I think this is happening because all the votes are 0

are you changing the values on a local script?
if you are then that is why it doesn’t work, it has to be changed on the server

I realized that so I changed the code to this:


but it keeps printing no map could be selected even though it shows that it has 1 vote.

that’s literally what I told you, all the votes are 0
my code worked fine
only reason it picked a random map was because you are most likely changing the votes in a local script(which is why they are 0)

the server script service script thinks all the votes are 0, because they are 0 on the server

I wasn’t changing them in a local script either. It said red map had 1 vote but it would print something else.

well looking at the screenshot you gave me I found a problem

you use table.sort() which goes from smallest to largest, if any of them are 0 it will be at the top

try

table.sort(mapTable, function(a, b)
    return a > b
end)

instead of

table.sort(mapTable)

OH I didn’t realize that. Thanks for telling me I’ll try this code.

You can try this model here. It worked for me :slight_smile: