Advertisement
HowToRoblox

VotingServer

Mar 21st, 2023
1,200
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.37 KB | None | 1 0
  1. local rs = game.ReplicatedStorage
  2.  
  3. local maps = rs:WaitForChild("Maps")
  4. local res = rs:WaitForChild("RemoteEvents")
  5.  
  6. local numMapsVoting = 3
  7. local intermissionTime = 5
  8. local voteTime = 10
  9.  
  10. local plrVotes = {}
  11.  
  12.  
  13. function addVote(plr:Player, mapName:string)
  14.    
  15.     plrVotes[plr] = mapName
  16.     res:WaitForChild("Voted"):FireAllClients(plrVotes)
  17. end
  18.  
  19. function removePlayerVote(plr:Player)
  20.    
  21.     plrVotes[plr] = nil
  22.     res:WaitForChild("Voted"):FireAllClients(plrVotes)
  23. end
  24.  
  25. function loadMap(mapName:string)
  26.    
  27.     local newMap = maps[mapName]:Clone()
  28.     newMap.Parent = workspace
  29.    
  30.     local spawns = newMap:WaitForChild("Spawns"):GetChildren()
  31.    
  32.     for i, plr in pairs(game.Players:GetPlayers()) do
  33.         if plr.Character then
  34.             plr.Character.HumanoidRootPart.CFrame = (spawns[i] and spawns[i].CFrame or spawns[i-#spawns]) + Vector3.new(0, 10, 0)
  35.         end
  36.     end
  37.    
  38.     return newMap
  39. end
  40.  
  41. function removeMap(map:Instance)
  42.    
  43.     map:Destroy()
  44.    
  45.     for _, plr in pairs(game.Players:GetPlayers()) do
  46.         plr:LoadCharacter()
  47.     end
  48. end
  49.  
  50. function handleRound()
  51.    
  52.     local plrsAlive = {}
  53.     for _, plr in pairs(game.Players:GetPlayers()) do
  54.        
  55.         if plr.Character and plr.Character.Humanoid.Health > 0 then
  56.             table.insert(plrsAlive, plr)
  57.            
  58.             plr.Character.Humanoid.Died:Connect(function()
  59.                 table.remove(plrsAlive, table.find(plrsAlive, plr))
  60.             end)
  61.         end
  62.     end
  63.    
  64.     for i = 1, 20 do
  65.         task.wait(1)
  66.         if #plrsAlive == 0 then
  67.             break
  68.         end
  69.     end
  70.    
  71.     task.wait(5)
  72. end
  73.  
  74.  
  75. res:WaitForChild("Voted").OnServerEvent:Connect(addVote)
  76.  
  77. game.Players.PlayerRemoving:Connect(removePlayerVote)
  78.  
  79.  
  80. while true do
  81.    
  82.     task.wait(intermissionTime)
  83.    
  84.     local mapsToVote = maps:GetChildren()
  85.    
  86.     while #mapsToVote > numMapsVoting do
  87.         table.remove(mapsToVote, math.random(1, #mapsToVote))
  88.     end
  89.    
  90.     plrVotes = {}
  91.    
  92.     res:WaitForChild("VotingBegun"):FireAllClients(mapsToVote)
  93.    
  94.     task.wait(voteTime)
  95.    
  96.     local highestVotedFor = nil
  97.    
  98.     local votes = {}
  99.     for i, map in pairs(mapsToVote) do
  100.         votes[map.Name] = 0
  101.        
  102.         if i == 1 then
  103.             highestVotedFor = map.Name
  104.         end
  105.     end
  106.    
  107.     for plr, vote in pairs(plrVotes) do
  108.        
  109.         if votes[vote] then
  110.             votes[vote] += 1
  111.            
  112.             if votes[highestVotedFor] < votes[vote] then
  113.                 highestVotedFor = vote
  114.             end
  115.         end
  116.     end
  117.    
  118.     res:WaitForChild("VotingEnded"):FireAllClients()
  119.    
  120.     local newMap = loadMap(highestVotedFor)
  121.    
  122.     handleRound()
  123.    
  124.     removeMap(newMap)
  125. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement