I have a map voting script here. This is the server side code:
local maps = ReplicatedStorage:WaitForChild("Maps")
local NumberOfMapOptions = 3
local intermissionTime = 5
local voteTime = 10
local plrVotes = {}
function addVote(plr, mapName)
plrVotes[plr.UserId] = mapName
ReplicatedStorage:WaitForChild("Voted"):FireAllClients(plrVotes)
end
function removePlayerVote(plr)
plrVotes[plr.UserId] = nil
ReplicatedStorage:WaitForChild("Voted"):FireAllClients(plrVotes)
end
function loadMap(mapName)
local mapmodels = maps:GetChildren()
local Map = {}
for i = 1, #mapmodels do
if mapmodels[i]:FindFirstChild("MapInfo") and mapmodels[i].MapInfo:FindFirstChild("MapName") and mapmodels[i].MapInfo.MapName.Value == mapName then
table.insert(Map, mapmodels[i])
end
end
if #Map == 0 then
warn("No map found with name: " .. tostring(mapName))
return nil
end
local newMap = Map[1]:Clone()
newMap.Parent = Workspace
return newMap
end
function removeMap(map)
if map and map.Parent then
map:Destroy()
end
local playerList = Players:GetPlayers()
for i = 1, #playerList do
local plr = playerList[i]
plr:LoadCharacter()
end
end
ReplicatedStorage:WaitForChild("Voted").OnServerEvent:Connect(function(plr, mapName)
addVote(plr, mapName)
end)
Players.PlayerRemoving:Connect(function(plr)
removePlayerVote(plr)
end)
function MapVote()
local mapsToVote = maps:GetChildren()
while #mapsToVote > NumberOfMapOptions do
table.remove(mapsToVote, math.random(1, #mapsToVote))
end
ReplicatedStorage:WaitForChild("VotingBegun"):FireAllClients(mapsToVote)
task.wait(18)
plrVotes = {}
ReplicatedStorage:WaitForChild("MapVoteTimer"):FireAllClients("")
if script:FindFirstChild("Sounds") and script.Sounds:FindFirstChild("MapVote") then
script.Sounds.MapVote:Play()
end
task.wait(voteTime)
for i = 1, 10 do
ReplicatedStorage:WaitForChild("MapVoteTimer"):FireAllClients(11 - i)
if script:FindFirstChild("Sounds") and script.Sounds:FindFirstChild("MapVoteTimer") then
script.Sounds.MapVoteTimer:Play()
end
task.wait(1)
end
ReplicatedStorage:WaitForChild("MapVoteTimer"):FireAllClients("")
if script:FindFirstChild("Sounds") and script.Sounds:FindFirstChild("MapVote") then
script.Sounds.MapVote:Stop()
end
local highestVotedFor = nil
local votes = {}
for i = 1, #mapsToVote do
local map = mapsToVote[i]
votes[map.Name] = 0
if i == 1 then
highestVotedFor = map.Name
end
end
for userId, vote in pairs(plrVotes) do
if votes[vote] ~= nil then
votes[vote] = votes[vote] + 1
if votes[highestVotedFor] < votes[vote] then
highestVotedFor = vote
end
end
end
ReplicatedStorage:WaitForChild("VotingEnded"):FireAllClients()
local newMap = loadMap(highestVotedFor)
return newMap
end
local currentMap = nil
And this is the client code
local rs = game.ReplicatedStorage
local frame = script.Parent
frame.Visible = false
-- Get the template frame for map options
local templateFrame = script:FindFirstChild("MapFrame")
local function getRandomUniqueMaps(maps, count)
local selected = {}
local used = {}
local total = #maps
if total == 0 then return selected end
if total >= count then
while #selected < count do
local idx = math.random(1, total)
if not used[idx] then
table.insert(selected, maps[idx])
used[idx] = true
end
end
else
for i = 1, total do
table.insert(selected, maps[i])
end
while #selected < count do
local idx = math.random(1, total)
table.insert(selected, maps[idx])
end
end
return selected
end
local votingBegunEvent = rs:FindFirstChild("VotingBegun")
if votingBegunEvent then
votingBegunEvent.OnClientEvent:Connect(function(mapsToVote)
task.wait(3)
frame.Visible = true
if frame:FindFirstChild("Vote for a map") then
frame["Vote for a map"].Visible = false
end
task.wait(5)
if frame:FindFirstChild("Vote for a map") then
frame["Vote for a map"].Visible = true
end
local mapOptions = frame:FindFirstChild("MapOptions")
if mapOptions and templateFrame then
local children = mapOptions:GetChildren()
for i = 1, #children do
local child = children[i]
if child.ClassName == templateFrame.ClassName then
child:Destroy()
end
end
local mapFrames = {}
local mapsToDisplay = getRandomUniqueMaps(mapsToVote, 3)
for i = 1, #mapsToDisplay do
local map = mapsToDisplay[i]
local newMapFrame = templateFrame:Clone()
newMapFrame.Name = map.MapInfo.MapName.Value
newMapFrame.MapName.Text = map.MapInfo.MapName.Value
newMapFrame.NumVotes.Text = "0"
newMapFrame.MapImage.Image = "rbxassetid://" .. map.MapInfo.ImageId.Value
newMapFrame.VoteButton.MouseButton1Click:Connect(function()
local votedEvent = rs:FindFirstChild("Voted")
if votedEvent then
votedEvent:FireServer(map.Name)
end
end)
table.insert(mapFrames, newMapFrame)
end
table.sort(mapFrames, function(a, b)
return a.Name < b.Name
end)
for i = 1, #mapFrames do
mapFrames[i].Parent = mapOptions
end
end
end)
end
local votingEndedEvent = rs:FindFirstChild("VotingEnded")
if votingEndedEvent then
votingEndedEvent.OnClientEvent:Connect(function()
local mapOptions = frame:FindFirstChild("MapOptions")
if mapOptions then
local MapFrames = mapOptions:GetChildren()
for i = 1, #MapFrames do
MapFrames[i]:Destroy()
end
end
task.wait(5)
frame.Visible = false
end)
end
local votedEvent = rs:FindFirstChild("Voted")
if votedEvent then
votedEvent.OnClientEvent:Connect(function(plrVotes)
if frame.Visible == true then
local votes = {}
for plr, vote in plrVotes do
if not votes[vote] then
votes[vote] = 1
else
votes[vote] = votes[vote] + 1
if script:FindFirstChild("Sound") then
script.Sound:Play()
end
end
end
local mapOptions = frame:FindFirstChild("MapOptions")
if mapOptions and templateFrame then
local children = mapOptions:GetChildren()
for i = 1, #children do
local mapFrame = children[i]
if mapFrame.ClassName == templateFrame.ClassName then
mapFrame.NumVotes.Text = tostring(votes[mapFrame.Name] or 0)
end
end
end
end
end)
end
local mapVoteTimerEvent = rs:FindFirstChild("MapVoteTimer")
if mapVoteTimerEvent then
mapVoteTimerEvent.OnClientEvent:Connect(function(text)
if frame:FindFirstChild("Timer") then
frame.Timer.Text = text
end
end)
end
The problem I am facing is that on the next time the code runs, there is only one map option to vote for, even though there were three to begin with, which is the normal amount.
Where is the bug being caused and how could it be fixed? I could really use some help. Thanks!