--[[
	New Hazmat Handler by Phoenix
	Woopsie
]]



local InChamber = {}
local State = "Normal" -- Normal, Hazard, Dead

local Check = function(Player)
	local Character = Player.Character
	
	if not Character:FindFirstChild("Torso") or not Character:FindFirstChild("Helmet") then
		repeat
			if not Character:FindFirstChild("Torso") or not Character:FindFirstChild("Helmet") and Character.Humanoid.Health > 0 then
				if State == "Normal" then
					Character.Humanoid.Health = Character.Humanoid.Health - 5
				elseif State == "Hazard" then
					Character.Humanoid.Health = Character.Humanoid.Health - 10
				elseif State == "Dead" then
					Character.Humanoid.Health = Character.Humanoid.Health - 20
				end
			end
			
			if Character.Humanoid.Health <= 0 then
				InChamber[Player]:Disconnect()
				InChamber[Player] = nil
				
				break
			end
			
			wait(1)
		until InChamber[Player] == nil
	elseif State == "Hazard" then
		repeat
			Character.Humanoid.Health = Character.Humanoid.Health - 1.5
			
			if Character.Humanoid.Health <= 0 then
				InChamber[Player]:Disconnect()
				InChamber[Player] = nil
				
				break
			end
			
			wait(2)
		until InChamber[Player] == nil
	elseif State == "Dead" then
		repeat
			Character.Humanoid.Health = Character.Humanoid.Health - 3
			
			if Character.Humanoid.Health <= 0 then
				InChamber[Player]:Disconnect()
				InChamber[Player] = nil
				
				break
			end
			
			wait(1)
		until InChamber[Player] == nil
	end
end

local Set = function(Player, SetState)
	if SetState == true then
		InChamber[Player] = Player.Character.Humanoid.Died:Connect(function()
			if InChamber[Player] ~= nil then
				InChamber[Player]:Disconnect()
			end
			
			InChamber[Player] = nil
		end)

		game:GetService("ReplicatedStorage").Game.Events.Game.Hazmat:FireClient(Player, true, State)

		Check(Player)
	else
		if InChamber[Player] ~= nil then
			InChamber[Player]:Disconnect()
			InChamber[Player] = nil
		end
		
		game:GetService("ReplicatedStorage").Game.Events.Game.Hazmat:FireClient(Player, false)
	end
end

game:GetService("ServerScriptService").Server.Events.HZReturnState.OnInvoke = function(Player)
	if InChamber[Player] ~= nil then
		return true
	end
	
	return false
end

game:GetService("ServerScriptService").Server.Events.HZSet.Event:Connect(function(Player, State)
	Set(Player, State)
end)

game:GetService("ServerScriptService").Server.Events.HZState.Event:Connect(function(SetState)
	if State ~= "Dead" then
		State = SetState
		
		for Player, NA in pairs(InChamber) do
			if Player ~= nil and NA ~= nil and Player.Character then
				if InChamber[Player] ~= nil then
					InChamber[Player]:Disconnect()
					InChamber[Player] = nil
				end

				delay(3, function() 
					InChamber[Player] = Player.Character.Humanoid.Died:Connect(function()
						if InChamber[Player] ~= nil then
							InChamber[Player]:Disconnect()
						end
						
						InChamber[Player] = nil
					end)
					
					game:GetService("ReplicatedStorage").Game.Events.Game.Hazmat:FireClient(Player, true, State)
					
					Check(Player)
				end)
			end
		end
	end
end)

game:GetService("ReplicatedStorage").Game.Events.Game.HazmatState.OnServerInvoke = function()
	return State
end

game:GetService("ServerStorage").Bindables:WaitForChild("Regen").Event:Connect(function()
	State = "Normal"
end)