SHARE
TWEET

Server

gavinn Mar 26th, 2020 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --Assets--
  2. local rep = game.ReplicatedStorage
  3. local assets = rep.Assets
  4.  
  5. --Maps--
  6. local maps = assets.Maps
  7.  
  8. --Signals--
  9. local signals = assets.Signals
  10. local event = signals.Event
  11. local fevent = signals.FEvent
  12.  
  13. --Static Variables--
  14. local mapVotes = {}
  15.  
  16. --Game Variables--
  17. local Game = workspace.Game
  18. local stats = Game.Stats
  19. local settings = {
  20.     mapVoteTime=15;
  21.     mapVoteDelay=120;
  22.     playersRequired=1;
  23. }
  24.  
  25. --Primary Events--
  26.  
  27. --[[
  28.     ARRAY = mapVotes
  29.         MapArray
  30.             Id
  31.             mapName
  32.             players
  33.                 players=UserId
  34. --]]
  35.  
  36. event.OnServerEvent:connect(function(player,variables)
  37.     if variables.reason == "voteOnMap" then
  38.         --First check if a player already voted on a map, if so change vote
  39.         for a,b in pairs(mapVotes) do
  40.             for d,c in pairs(b.players) do
  41.                 if c.player == player.UserId then
  42.                     table.remove(b.players,d)
  43.                     break
  44.                 end
  45.             end
  46.         end
  47.         --Add player to the map votes.
  48.         for a,b in pairs(mapVotes) do
  49.             if b.id == variables.itemNum then
  50.                 table.insert(b.players,{player=player.UserId})
  51.             end
  52.         end
  53.     elseif variables.reason == "removeFromVote" then
  54.         for a,b in pairs(mapVotes) do
  55.             for d,c in pairs(b.players) do
  56.                 if c.player == player.UserId then
  57.                     table.remove(b.players, d)
  58.                     break
  59.                 end
  60.             end
  61.         end
  62.     end
  63. end)
  64.  
  65. function getMap()
  66.     local randomMap = maps:GetChildren()[math.random(1,#maps:GetChildren())]
  67.     for a,b in pairs(mapVotes) do
  68.         if b.mapName == randomMap.Name then
  69.             return getMap()
  70.         end
  71.     end
  72.     return randomMap.Name
  73. end
  74.  
  75. while wait() do
  76.     if #game.Players:GetPlayers() < settings.playersRequired then
  77.         local playersNeeded = settings.playersRequired-#game.Players:GetPlayers()
  78.         stats.Status.Value = playersNeeded.." "..((playersNeeded==1 and "Player")or "Players").." needed to play!"
  79.     else
  80.         --Assign maps
  81.         mapVotes={}
  82.         for a,b in pairs(Game.Special.VotingPads:GetChildren()) do
  83.             table.insert(mapVotes, {id=tonumber(b.Name:match("%d+")); MapName=getMap(); players={};})
  84.         end
  85.    
  86.         --Fire clients, start sending in votes.
  87.         event:FireAllClients({reason="startVoting"})   
  88.        
  89.         --Do main loop
  90.         local start = tick()
  91.         while wait() do
  92.             if tick()-start >= settings.mapVoteTime then break end
  93.             local secondsLeft = math.floor(settings.mapVoteTime-(tick()-start))
  94.             stats.Status.Value = secondsLeft.." "..((secondsLeft==1 and "Second") or "Seconds").." Left to Vote!"
  95.             for a,b in pairs(Game.Special.VotingPads:GetChildren()) do
  96.                 local playersVoting, mName
  97.                 for d,c in pairs(mapVotes) do
  98.                     if c.id == tonumber(b.Name:match("%d+")) then
  99.                         playersVoting=#c.players
  100.                         mName=c.MapName
  101.                         break
  102.                     end
  103.                 end
  104.                 b.sGui.Title.Text = mName.." - "..playersVoting.." "..((playersVoting==1 and "Voter") or "Voters")
  105.             end
  106.         end
  107.        
  108.         --Get winner
  109.         table.sort(mapVotes, function(a,b) return #a.players>#b.players end)
  110.        
  111.         for a,b in pairs(Game.Special.VotingPads:GetChildren()) do
  112.             if tonumber(b.Name:match("%d+")) == mapVotes[1].id then
  113.                 b.sGui.Title.Text = "Winner!"
  114.                 stats.Status.Value = mapVotes[1].MapName.." has been chosen!"
  115.             else
  116.                 b.sGui.Title.Text = "Lost Vote!"
  117.             end
  118.         end
  119.        
  120.         wait(settings.mapVoteDelay)
  121.     end
  122. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top