Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -------------------
- --| WaitForChild |--
- --------------------
- -- Waits for parent.child to exist, then returns it
- local function WaitForChild(parent, childName)
- assert(parent, "ERROR: WaitForChild: parent is nil")
- while not parent:FindFirstChild(childName) do parent.ChildAdded:wait() end
- return parent[childName]
- end
- -----------------
- --| Variables |--
- -----------------
- local PlayersService = Game:GetService('Players')
- local SpawnAllPlayers = WaitForChild(script, 'SpawnAllPlayers')
- local ClearAllPlayers = WaitForChild(script, 'ClearAllPlayers')
- local KillAllPlayers = WaitForChild(script, 'KillAllPlayers')
- local Enable = WaitForChild(script, 'Enable')
- local Disable = WaitForChild(script, 'Disable')
- local RespawnWaveTime = 0
- local MinimumRespawnTime = 0
- local StartTime = 0
- local Enabled = false
- local DisableSpawnOnJoin = false
- local OriginalSetting = nil
- local NewPlayerConnection = nil
- local Connections = {}
- -------------------------
- --| Utility Functions |--
- -------------------------
- -- Custom logic for Died: wait for next wave, then respawn
- local function OnDied(player)
- -- Wait until the next respawn wave
- local timeUntilNextWave = RespawnWaveTime - ((time() - StartTime) % RespawnWaveTime)
- if timeUntilNextWave < MinimumRespawnTime then -- The next wave is too soon, wait until the next-next one
- timeUntilNextWave = timeUntilNextWave + RespawnWaveTime
- end
- wait(timeUntilNextWave)
- -- Respawn the player if we're still enabled and they're still in the game
- if Enabled and player and player.Parent == PlayersService then
- player:LoadCharacter()
- end
- end
- -- Connects OnDied to Humanoid Died, if possible
- local function ConnectOnDied(player)
- if player.Character then
- local humanoid = player.Character:FindFirstChild('Humanoid')
- if humanoid then
- humanoid.Died:connect(function() OnDied(player) end)
- end
- end
- end
- -- Calls ConnectOnDied once now, and again every time the player spawns. Returns spawn connection
- local function ConnectPlayer(player)
- ConnectOnDied(player)
- return player.CharacterAdded:connect(function() ConnectOnDied(player) end)
- end
- -- Spawns new players (unless disabled), calls ConnectPlayer and saves their connection
- local function OnPlayerAdded(player)
- if not DisableSpawnOnJoin then
- player:LoadCharacter()
- end
- Connections[player] = ConnectPlayer(player)
- end
- --------------------------
- --| Bindable Functions |--
- --------------------------
- -- SpawnAllPlayers: Loads all player characters
- SpawnAllPlayers.OnInvoke = function()
- for _, player in pairs(PlayersService:GetPlayers()) do
- player:LoadCharacter()
- end
- end
- -- ClearAllPlayers: Destroys all player characters
- ClearAllPlayers.OnInvoke = function()
- for _, player in pairs(PlayersService:GetPlayers()) do
- player.Character = nil
- end
- end
- -- KillAllPlayers: Kills all player characters
- KillAllPlayers.OnInvoke = function()
- for _, player in pairs(PlayersService:GetPlayers()) do
- if player.Character then
- local humanoid = player.Character:FindFirstChild('Humanoid')
- if humanoid then
- humanoid.Health = 0
- end
- end
- end
- end
- -- Enable: Initializes variables, disables default respawning, and connects to all players
- Enable.OnInvoke = function(respawnWaveTime, minimumRespawnTime, disableSpawnOnJoin)
- -- Initialize variables
- RespawnWaveTime = respawnWaveTime or 5 -- Seconds between each respawn wave
- MinimumRespawnTime = minimumRespawnTime or 3 -- Players must wait at least this long to respawn
- DisableSpawnOnJoin = disableSpawnOnJoin or false -- Players will spawn when they join unless this is specified
- StartTime = time()
- Enabled = true
- -- Disable default respawning but save original setting
- OriginalSetting = PlayersService.CharacterAutoLoads
- PlayersService.CharacterAutoLoads = false
- -- Connect respawning functionality for all players, current and future
- NewPlayerConnection = PlayersService.PlayerAdded:connect(OnPlayerAdded)
- for _, player in pairs(PlayersService:GetPlayers()) do
- OnPlayerAdded(player)
- end
- end
- -- Disable: Turns off wave spawning and restores setting for default respawning
- Disable.OnInvoke = function()
- if Enabled then
- PlayersService.CharacterAutoLoads = OriginalSetting
- -- Disconnect all saved connections
- NewPlayerConnection:disconnect()
- NewPlayerConnection = nil
- for _, connection in pairs(Connections) do
- connection:disconnect()
- end
- Connections = {}
- Enabled = false
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement