Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  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.  
  15. local mapVotes = {}
  16.  
  17. --game variables
  18.  
  19. local Game = workspace.Game
  20. local stats = Game.Stats
  21. local settings = {
  22. mapVoteTime=15;
  23. mapVoteDelay=3;
  24. playersRequired=1
  25. }
  26.  
  27. --primary events
  28.  
  29.  
  30.  
  31. event.OnServerEvent:connect(function(player, variables)
  32. if variables.reason == "voteOnMap" then
  33. --first check if a player already voted on a map, if so change vote
  34. for a,b in pairs(mapVotes) do
  35. for d,c in pairs(b.players) do
  36. if c.player == player.UserId then
  37. table.remove(b.players, d)
  38. break
  39. end
  40. end
  41. end
  42. --add player to the map votes
  43. for a,b in pairs(mapVotes) do
  44. if b.id == variables.itemNum then
  45. table.insert(b.players, {player=player.UserId})
  46. end
  47. end
  48. elseif variables.reason == "removeFromVote" then
  49. for a,b in pairs(mapVotes) do
  50. for d,c in pairs(b.players) do
  51. if c.player == player.UserId then
  52. table.remove(b.players, d)
  53. break
  54. end
  55. end
  56. end
  57. end
  58. end)
  59.  
  60. function getMap()
  61. local randomMap = maps:GetChildren()[math.random(1,#maps:GetChildren())]
  62. for a,b in pairs(mapVotes) do
  63. if b.mapName == randomMap.Name then
  64. return getMap()
  65. end
  66. end
  67. return randomMap.Name
  68. end
  69.  
  70. while wait() do
  71. if #game.Players:GetPlayers() < settings.playersRequired then
  72. local playersNeeded = settings.playersRequired-#game.Players:GetPlayers()
  73. stats.Status.Value = playersNeeded..""..((playersNeeded==1 and "Player") or "Players").." needed to play!"
  74. else
  75. --assign maps
  76. mapVotes={}
  77. for a,b in pairs(Game.Special.VotingPads:GetChildren()) do
  78. table.insert(mapVotes, {id=tonumber(b.Name:match("%d+")); mapName=getMap(); players={};})
  79. end
  80. --fire clients, start sending in votes
  81. event:FireAllClients({reason="startVoting"})
  82.  
  83. -- do main loop
  84. local start = tick()
  85. while wait() do
  86. if tick()-start >= settings.mapVoteTime then break end
  87. local secondsLeft = math.floor(settings.mapVoteTime-(tick()-start))
  88. stats.Status.Value = secondsLeft.." "..((secondsLeft==1 and "second") or "seconds").." left to vote!"
  89. for a,b in pairs(Game.Special.VotingPads:GetChildren()) do
  90. local playersVoting, mName
  91. for d,c in pairs(mapVotes) do
  92. if c.id == tonumber(b.Name:match("%d+")) then
  93. playersVoting=#c.players
  94. mName=c.mapName
  95. break
  96. end
  97. end
  98. b.sGui.Title.Text = mName.." - "..playersVoting.." "..((playersVoting==1 and " Voter") or " Voters")
  99. end
  100. end
  101.  
  102. --get winner
  103. table.sort(mapVotes, function(a,b) return #a.players>#b.players end)
  104.  
  105. for a,b in pairs(Game.Special.VotingPads:GetChildren()) do
  106. if tonumber(b.Name:match("%d+")) == mapVotes[1].id then
  107. b.sGui.Title.Text = "Winner!"
  108. stats.Status.Value = mapVotes[1].mapName.." was chosen!"
  109. else
  110. b.sGui.Title.Text = "Lost Vote!"
  111. end
  112. end
  113.  
  114. wait(settings.mapVoteDelay)
  115. end
  116. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement