Hide block of HTML if the users role is a certain role

Try to change this code

 // Get the user object
    $user = new WP_User( get_current_user_id() );
    $index = key( $user->roles );
    $user_role = $user->roles[ $index ];

// User already got that user
if ( $user_role == $role ) {

    echo sprintf( __( 'You already have %s role.' ), $role );

} else {

    // update user role
    $user->set_role( $role );
    echo sprintf( __( 'Your role was changed to %s.' ), $role );

}

to the following

//Get the user
$user = wp_get_current_user();

// User already got this role
if( $user->has_cap( $role ) ) {

  echo sprintf( __( 'You already have %s role.' ), $role );

  // Do whatever you want

} else {

  // update user role
  $user->set_role( $role );
  echo sprintf( __( 'Your role was changed to %s.' ), $role );

}