Advertisement
Guest User

Untitled

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