Advertisement
leaspect489

Untitled

Jan 3rd, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.11 KB | None | 0 0
  1. -------------------
  2. --| WaitForChild |--
  3. --------------------
  4.  
  5. -- Waits for parent.child to exist, then returns it
  6. local function WaitForChild(parent, childName)
  7. assert(parent, "ERROR: WaitForChild: parent is nil")
  8. while not parent:FindFirstChild(childName) do parent.ChildAdded:wait() end
  9. return parent[childName]
  10. end
  11.  
  12. -----------------
  13. --| Variables |--
  14. -----------------
  15.  
  16. local PlayersService = Game:GetService('Players')
  17.  
  18. local SpawnAllPlayers = WaitForChild(script, 'SpawnAllPlayers')
  19. local ClearAllPlayers = WaitForChild(script, 'ClearAllPlayers')
  20. local KillAllPlayers = WaitForChild(script, 'KillAllPlayers')
  21. local Enable = WaitForChild(script, 'Enable')
  22. local Disable = WaitForChild(script, 'Disable')
  23.  
  24. local RespawnWaveTime = 0
  25. local MinimumRespawnTime = 0
  26. local StartTime = 0
  27.  
  28. local Enabled = false
  29. local DisableSpawnOnJoin = false
  30.  
  31. local OriginalSetting = nil
  32. local NewPlayerConnection = nil
  33. local Connections = {}
  34.  
  35. -------------------------
  36. --| Utility Functions |--
  37. -------------------------
  38.  
  39. -- Custom logic for Died: wait for next wave, then respawn
  40. local function OnDied(player)
  41. -- Wait until the next respawn wave
  42. local timeUntilNextWave = RespawnWaveTime - ((time() - StartTime) % RespawnWaveTime)
  43. if timeUntilNextWave < MinimumRespawnTime then -- The next wave is too soon, wait until the next-next one
  44. timeUntilNextWave = timeUntilNextWave + RespawnWaveTime
  45. end
  46. wait(timeUntilNextWave)
  47.  
  48. -- Respawn the player if we're still enabled and they're still in the game
  49. if Enabled and player and player.Parent == PlayersService then
  50. player:LoadCharacter()
  51. end
  52. end
  53.  
  54. -- Connects OnDied to Humanoid Died, if possible
  55. local function ConnectOnDied(player)
  56. if player.Character then
  57. local humanoid = player.Character:FindFirstChild('Humanoid')
  58. if humanoid then
  59. humanoid.Died:connect(function() OnDied(player) end)
  60. end
  61. end
  62. end
  63.  
  64. -- Calls ConnectOnDied once now, and again every time the player spawns. Returns spawn connection
  65. local function ConnectPlayer(player)
  66. ConnectOnDied(player)
  67. return player.CharacterAdded:connect(function() ConnectOnDied(player) end)
  68. end
  69.  
  70. -- Spawns new players (unless disabled), calls ConnectPlayer and saves their connection
  71. local function OnPlayerAdded(player)
  72. if not DisableSpawnOnJoin then
  73. player:LoadCharacter()
  74. end
  75. Connections[player] = ConnectPlayer(player)
  76. end
  77.  
  78. --------------------------
  79. --| Bindable Functions |--
  80. --------------------------
  81.  
  82. -- SpawnAllPlayers: Loads all player characters
  83. SpawnAllPlayers.OnInvoke = function()
  84. for _, player in pairs(PlayersService:GetPlayers()) do
  85. player:LoadCharacter()
  86. end
  87. end
  88.  
  89. -- ClearAllPlayers: Destroys all player characters
  90. ClearAllPlayers.OnInvoke = function()
  91. for _, player in pairs(PlayersService:GetPlayers()) do
  92. player.Character = nil
  93. end
  94. end
  95.  
  96. -- KillAllPlayers: Kills all player characters
  97. KillAllPlayers.OnInvoke = function()
  98. for _, player in pairs(PlayersService:GetPlayers()) do
  99. if player.Character then
  100. local humanoid = player.Character:FindFirstChild('Humanoid')
  101. if humanoid then
  102. humanoid.Health = 0
  103. end
  104. end
  105. end
  106. end
  107.  
  108. -- Enable: Initializes variables, disables default respawning, and connects to all players
  109. Enable.OnInvoke = function(respawnWaveTime, minimumRespawnTime, disableSpawnOnJoin)
  110. -- Initialize variables
  111. RespawnWaveTime = respawnWaveTime or 5 -- Seconds between each respawn wave
  112. MinimumRespawnTime = minimumRespawnTime or 3 -- Players must wait at least this long to respawn
  113. DisableSpawnOnJoin = disableSpawnOnJoin or false -- Players will spawn when they join unless this is specified
  114. StartTime = time()
  115. Enabled = true
  116.  
  117. -- Disable default respawning but save original setting
  118. OriginalSetting = PlayersService.CharacterAutoLoads
  119. PlayersService.CharacterAutoLoads = false
  120.  
  121. -- Connect respawning functionality for all players, current and future
  122. NewPlayerConnection = PlayersService.PlayerAdded:connect(OnPlayerAdded)
  123. for _, player in pairs(PlayersService:GetPlayers()) do
  124. OnPlayerAdded(player)
  125. end
  126. end
  127.  
  128. -- Disable: Turns off wave spawning and restores setting for default respawning
  129. Disable.OnInvoke = function()
  130. if Enabled then
  131. PlayersService.CharacterAutoLoads = OriginalSetting
  132.  
  133. -- Disconnect all saved connections
  134. NewPlayerConnection:disconnect()
  135. NewPlayerConnection = nil
  136. for _, connection in pairs(Connections) do
  137. connection:disconnect()
  138. end
  139. Connections = {}
  140.  
  141. Enabled = false
  142. end
  143. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement