There are twelve maps, with three options to vote from. The issue I am having is that every time the round starts and the map vote begins, the same three map choices appear, and none of the other ones. I don’t know exactly why, but here is the code. All help is appreciated.
local plrVotes = {}
numMapsVoting = 3
function addVote(plr:Player, mapName:string)
plrVotes[plr] = mapName
game.ReplicatedStorage:WaitForChild("Voted"):FireAllClients(plrVotes)
end
function removePlayerVote(plr:Player)
plrVotes[plr] = nil
game.ReplicatedStorage.WaitForChild("Voted"):FireAllClients(plrVotes)
end
function mapvote()
MapsFolder = game.Lighting.Maps
Maps = MapsFolder:GetChildren()
mapsToVote = Maps
while #mapsToVote > numMapsVoting do
table.remove(mapsToVote, math.random(1, #mapsToVote))
end
plrVotes = {}
game.ReplicatedStorage:WaitForChild("VotingBegun"):FireAllClients(mapsToVote)
task.wait(20)
local highestVotedFor = nil
local votes = {}
for i, map in pairs(mapsToVote) do
votes[map.Name] = 0
if i == 1 then
highestVotedFor = map.Name
end
end
for plr, vote in pairs(plrVotes) do
if votes[vote] then
votes[vote] += 1
if votes[highestVotedFor] < votes[vote] then
highestVotedFor = vote
end
end
end
game.ReplicatedStorage:WaitForChild("VotingEnded"):FireAllClients()
newMap = highestVotedFor
end
game.ReplicatedStorage:WaitForChild("Voted").OnServerEvent:Connect(addVote)
game.Players.PlayerRemoving:Connect(removePlayerVote)
1 Like
Where are you calling the mapvote function?
I use the following to select a random map, perhaps not the most efficient but it works OK for me:
maps = MapsFolder:GetChildren()
local keysTable = {}
for key, value in ipairs(maps) do
table.insert(keysTable, key)
end
local randomKey1 = keysTable[math.random(#keysTable)]
local mapChosen1 = maps[randomKey1]
local randomKey2, mapChosen2
repeat
randomKey2 = keysTable[math.random(#keysTable)]
mapChosen2 = maps[randomKey2]
until randomKey2 ~= randomKey1
local randomKey3, mapChosen3
repeat
randomKey3 = keysTable[math.random(#keysTable)]
mapChosen3 = maps[randomKey3]
until randomKey3 ~= randomKey1 and randomKey3 ~= randomKey2
I call it in here:
while true do
for _, plr in pairs(game.Players:GetPlayers()) do
plr:LoadCharacter()
end
game.ReplicatedStorage.MessageVisible.Value = true
game.ReplicatedStorage.MessageText.Value = "Intermission"
timeEvent = game.ReplicatedStorage.TimeEvent
function Timer()
local timeAmount = 15
while timeAmount > 0 do
timeEvent:FireAllClients(timeAmount)
wait(1)
timeAmount -= 1
end
end
Timer()
game.ReplicatedStorage.MessageVisible.Value = false
Gamemode = getNextGamemode()
GamemodeClone = game.Lighting.Gamemodes[Gamemode]:Clone()
startNewRound()
game.ReplicatedStorage.RoundStartGuiV.Value = true
wait(5)
mapvote()
wait(3)
local MapClone = MapsFolder[newMap]:Clone()
MapClone.Parent = game.Workspace.Map
MapClone:MakeJoints()
wait(3)
for i, v in pairs(game.Players:GetPlayers()) do
local randomSpawn = MapClone.TeleportSpots:GetChildren()[Random.new():NextInteger(1, #MapClone.TeleportSpots:GetChildren())]
v.Character.HumanoidRootPart.CFrame = randomSpawn.CFrame
end
game.ReplicatedStorage.RoundStartGuiV.Value = false
if GamemodeClone.Name == "Gamemode1" then
--gamemode code here--
end
end
startNewRound()
game.ReplicatedStorage.RoundStartGuiV.Value = true
wait(5)
mapvote()
wait(3)
When put together, the code looks like this
local plrVotes = {}
numMapsVoting = 3
function addVote(plr:Player, mapName:string)
plrVotes[plr] = mapName
game.ReplicatedStorage:WaitForChild("Voted"):FireAllClients(plrVotes)
end
function removePlayerVote(plr:Player)
plrVotes[plr] = nil
game.ReplicatedStorage.WaitForChild("Voted"):FireAllClients(plrVotes)
end
function mapvote()
MapsFolder = game.Lighting.Maps
Maps = MapsFolder:GetChildren()
mapsToVote = Maps
while #mapsToVote > numMapsVoting do
table.remove(mapsToVote, math.random(1, #mapsToVote))
end
plrVotes = {}
game.ReplicatedStorage:WaitForChild("VotingBegun"):FireAllClients(mapsToVote)
task.wait(20)
local highestVotedFor = nil
local votes = {}
for i, map in pairs(mapsToVote) do
votes[map.Name] = 0
if i == 1 then
highestVotedFor = map.Name
end
end
for plr, vote in pairs(plrVotes) do
if votes[vote] then
votes[vote] += 1
if votes[highestVotedFor] < votes[vote] then
highestVotedFor = vote
end
end
end
game.ReplicatedStorage:WaitForChild("VotingEnded"):FireAllClients()
newMap = highestVotedFor
end
game.ReplicatedStorage:WaitForChild("Voted").OnServerEvent:Connect(addVote)
game.Players.PlayerRemoving:Connect(removePlayerVote)
while true do
for _, plr in pairs(game.Players:GetPlayers()) do
plr:LoadCharacter()
end
game.ReplicatedStorage.MessageVisible.Value = true
game.ReplicatedStorage.MessageText.Value = "Intermission"
timeEvent = game.ReplicatedStorage.TimeEvent
function Timer()
local timeAmount = 15
while timeAmount > 0 do
timeEvent:FireAllClients(timeAmount)
wait(1)
timeAmount -= 1
end
end
Timer()
game.ReplicatedStorage.MessageVisible.Value = false
Gamemode = getNextGamemode()
GamemodeClone = game.Lighting.Gamemodes[Gamemode]:Clone()
startNewRound()
game.ReplicatedStorage.RoundStartGuiV.Value = true
wait(5)
mapvote()
wait(3)
local MapClone = MapsFolder[newMap]:Clone()
MapClone.Parent = game.Workspace.Map
MapClone:MakeJoints()
wait(3)
for i, v in pairs(game.Players:GetPlayers()) do
local randomSpawn = MapClone.TeleportSpots:GetChildren()[Random.new():NextInteger(1, #MapClone.TeleportSpots:GetChildren())]
v.Character.HumanoidRootPart.CFrame = randomSpawn.CFrame
end
game.ReplicatedStorage.RoundStartGuiV.Value = false
if GamemodeClone.Name == "Gamemode1" then
--gamemode code here
end
end
lol this is HTR code, you might want to copy it fully and understand how he does it not just copy paste parts… go back to his model and just copy all of it, not just a few bits and parts because your missing some things.
Remember: Sub2HTR https://create.roblox.com/store/asset/12856772219/Map-Voting-Gui?externalSource=www