Guest User

Untitled

a guest
Oct 27th, 2020
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.40 KB | None | 0 0
  1. local minigameModule = {
  2. gameRunning = false,
  3. playersAlive = {},
  4. currentMap = nil
  5. }
  6.  
  7. local waitForChild = game.WaitForChild
  8. local findFirstChild = game.FindFirstChild
  9.  
  10. -- Modules
  11.  
  12. local settingsModule = require(waitForChild(script, "Settings"))
  13. local onWin = (function()
  14. local onWinModule = findFirstChild(script, "OnWin")
  15.  
  16. if onWinModule then
  17. local onWinFunction = require(onWinModule)
  18.  
  19. if type(onWinFunction) == "function" then
  20. return onWinFunction
  21. end
  22. end
  23. end)()
  24.  
  25. local remoteEvent = waitForChild(game:GetService("ReplicatedStorage"), "Event")
  26. local mapsStorage = waitForChild(game:GetService("ServerStorage"), "Maps"):GetChildren()
  27.  
  28. local playersService = game:GetService("Players")
  29.  
  30. function minigameModule.isPotentialGame()
  31. return playersService.NumPlayers >= settingsModule.minimumPlayers
  32. end
  33.  
  34. function minigameModule:chooseMap()
  35. local chosenMap = mapsStorage[#mapsStorage]:Clone()
  36. if findFirstChild(chosenMap, "Spawns") then
  37. chosenMap.Parent = workspace
  38. chosenMap:MakeJoints()
  39. self.currentMap = chosenMap
  40. return chosenMap
  41. end
  42. end
  43.  
  44. function minigameModule:spawnPlayers()
  45. local playersAlive = self.playersAlive
  46. local spawns = self.currentMap.Spawns:GetChildren()
  47. for index = 1, #playersAlive do
  48. local playerData = playersAlive[index]
  49. playerData.playerHumanoidRoot.CFrame = spawns[math.random(#spawns)].CFrame
  50. end
  51. end
  52.  
  53. function minigameModule:runIntermission()
  54. if settingsModule.intermissionTime > 0 then
  55. for currentTime = math.floor(settingsModule.intermissionTime), 0, -1 do
  56. remoteEvent:FireAllClients("Timer", currentTime)
  57. wait(1)
  58. end
  59. end
  60. remoteEvent:FireAllClients("CeaseGUIs")
  61. end
  62.  
  63. function minigameModule:getAlivePlayers()
  64. local playersAlive = {}
  65. for index, currentPlayer in next, playersService:GetPlayers() do
  66. local playerCharacter = currentPlayer.Character
  67. if playerCharacter then
  68. local playerHumanoidRoot = findFirstChild(playerCharacter, "HumanoidRootPart")
  69. local playerHumanoid = findFirstChild(playerCharacter, "Humanoid")
  70. if playerHumanoid and playerHumanoidRoot then
  71. table.insert(playersAlive, {
  72. player = currentPlayer,
  73. playerHumanoid = playerHumanoid,
  74. playerHumanoidRoot = playerHumanoidRoot
  75. })
  76. end
  77. end
  78. end
  79. return playersAlive
  80. end
  81.  
  82. function minigameModule:isLegalGame()
  83. if #self:getAlivePlayers() >= settingsModule.minimumPlayers then
  84. return true
  85. end
  86. end
  87.  
  88. function minigameModule:queryGameStart()
  89. if self.gameRunning then
  90. return
  91. elseif self.isPotentialGame() then
  92. self.gameRunning = true
  93. remoteEvent:FireAllClients("CeaseGUIs")
  94. self:runIntermission()
  95.  
  96. if self:isLegalGame() then
  97. if settingsModule.roundDuration > 0 then
  98.  
  99. local currentMap = self:chooseMap()
  100. local mapWeapons = findFirstChild(currentMap, "Weapons")
  101.  
  102. local playersAlive = self:getAlivePlayers()
  103. self.playersAlive = playersAlive
  104.  
  105. for index = 1, #playersAlive do
  106. local currentPlayer = playersAlive[index]
  107. local backpack = findFirstChild(currentPlayer.player, "Backpack")
  108.  
  109. if backpack and mapWeapons then
  110. for index, weapon in next, mapWeapons:GetChildren() do
  111. weapon:Clone().Parent = backpack
  112. end
  113. end
  114.  
  115. local connection
  116. connection = currentPlayer.playerHumanoid.Died:connect(function()
  117. connection:disconnect()
  118. table.remove(playersAlive, index)
  119. if #playersAlive < 2 then
  120. local winner = playersAlive[1]
  121. if winner then
  122. self:endGame(winner.player.Name .. " has won!", winner.player)
  123. else
  124. self:endGame("No one has won!")
  125. end
  126. end
  127. end)
  128. end
  129.  
  130. if mapWeapons then
  131. mapWeapons:Destroy()
  132. end
  133.  
  134. self:spawnPlayers()
  135.  
  136. remoteEvent:FireAllClients("Message", currentMap.Name .. " was chosen!", 5)
  137.  
  138. for currentTime = settingsModule.roundDuration, 0, -1 do
  139. if not self.gameRunning then
  140. return
  141. end
  142. remoteEvent:FireAllClients("Timer", currentTime)
  143. wait(1)
  144. end
  145. self:endGame("The timer ran out! No one has won!")
  146. end
  147.  
  148. else
  149. self:endGame("Not enough players alive to begin the round!")
  150. end
  151. else
  152. local remainingPlayers = settingsModule.minimumPlayers - playersService.NumPlayers
  153. remoteEvent:FireAllClients("Message", "Waiting for " .. remainingPlayers .. " player" .. (remainingPlayers > 1 and "s" or "") .. " to join.")
  154. end
  155. end
  156.  
  157. function minigameModule:endGame(outputMessage, winner)
  158. if self.gameRunning then
  159.  
  160. self.gameRunning = false
  161. self.currentMap:Destroy()
  162. self.currentMap = nil
  163.  
  164. if winner and onWin then
  165. onWin(winner)
  166. end
  167.  
  168. for index, player in next, playersService:GetPlayers() do
  169. player:LoadCharacter()
  170. end
  171.  
  172. wait(1)
  173.  
  174. remoteEvent:FireAllClients("Message", outputMessage, 5)
  175.  
  176. wait(5)
  177.  
  178. self:queryGameStart()
  179. end
  180. end
  181.  
  182. function minigameModule:removePlayer(player)
  183. if self.gameRunning then
  184. for index = 1, #self.playersAlive do
  185. if self.playersAlive[index].player == player then
  186. table.remove(self.playersAlive, index)
  187. if #self.playersAlive <= 1 then
  188. self:endGame("Not enough players to continue the game.")
  189. end
  190. break
  191. end
  192. end
  193. end
  194. end
  195.  
  196. playersService.PlayerAdded:connect(function()
  197. minigameModule:queryGameStart()
  198. end)
  199.  
  200. playersService.PlayerRemoving:connect(function(player)
  201. minigameModule:removePlayer(player)
  202. end)
Add Comment
Please, Sign In to add comment