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
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
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)
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